[android学习]android_gps定位服务简单实现
前言
gps定位服务的学习是这段时间gps课程的学习内容,之前老师一直在将概念,今天终于是实践课(其实就是给了一个案例,让自己照着敲).不过在照着案列敲了两遍之后,发现老师的案例是在是太老了,并且直接照着案例敲,也无法理解其中很多类,方法的作用.
于是自己在网上查看了其他实现的方法,并尝试敲案列,期间的挫折一言难尽.(网上找的案例也并不信息,使得我在给予权限,和权限检查方面一直报错,因为我使用的是最新的As和java11,在经过数遍从基础理解到实例编写的过程和不知多少遍google之后,终于完成了这次练习)总结起来:
还是发现自己有不少的问题,在代码的理解能力上经过了这段时间的学习确实有些长进,但在较复杂的语句上面,理解还是有不小的困难.
其次,在没有事先了解学习某些类之前,是真的不适合直接照案例敲和学习(没有十分详细注释的案例,通常情况下都是如此),其效率实在低下,且很多时候会不知所云.
(个人并不提倡照着敲,敲的多了自然就懂了的学习说法,或许它只是针对于懒的人,亦或许这种说法其实只是一个劝诫我们勤奋努力,多实践的比喻.).
- 源代码
- activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="@drawable/gps">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:text="您的gps定位信息是:"
android:textColor="#fff"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="5dp"
android:text="经度:"
android:textSize="22dp"
android:textColor="#fff"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="5dp"
android:text="维度:"
android:textColor="#fff"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="5dp"
android:text="速度:"
android:textSize="22dp"
android:textColor="#fff"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="5dp"
android:text="海拔:"
android:textSize="22dp"
android:textColor="#fff"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="5dp"
android:text="方位:"
android:textSize="22dp"
android:textColor="#fff"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="5dp"
android:text="时间:"
android:textSize="22dp"
android:textColor="#fff"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="5dp"
android:text="卫星数:"
android:textSize="22dp"
android:textColor="#fff"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="22dp"
android:layout_marginLeft="10dp"
android:text="打开"/>
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textSize="22dp"
android:text="关闭"/>
</LinearLayout>
</LinearLayout>
- MainActivity.java
package cn.gemuxiaoshe.gpsapplication20;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import cn.gemuxiaoshe.gpsapplication20.Tools.Timetool;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private LocationManager lm;
private TextView tv_1, tv_2, tv_3, tv_4, tv_5, tv_6, tv_7;
private Button btn1, btn2;
private List<GpsSatellite> numSatlliteList = new ArrayList<GpsSatellite>(); // 卫星信号
private Location location;
private LocationListener locationListener= new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// 当gps定位信息发生改变时,更新定位
updateShow(location);
}
@Override
public void onProviderEnabled(String provider) {
// 当gpsLocationProvider可用时,更新定位
updateShow(null);
}
@Override
public void onProviderDisabled(String s) {
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.start);
btn2 = (Button)findViewById(R.id.stop);
tv_1 = (TextView) findViewById(R.id.tv_1);
tv_2 = (TextView) findViewById(R.id.tv_2);
tv_3 = (TextView) findViewById(R.id.tv_3);
tv_4 = (TextView) findViewById(R.id.tv_4);
tv_5 = (TextView) findViewById(R.id.tv_5);
tv_6 = (TextView) findViewById(R.id.tv_6);
tv_7 = (TextView) findViewById(R.id.tv_7);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
//点击事件
public void onClick(View v) {
if (v == btn1) {
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, "gps已关闭,请手动打开再试!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "gps定位中...", Toast.LENGTH_SHORT).show();
}
// new 对象设置精度信息
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(true);
criteria.setBearingRequired(true);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = lm.getBestProvider(criteria, true);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
location = lm.getLastKnownLocation(provider);
updateShow(location);
//设置动态监听,时间为两秒
lm.requestLocationUpdates(provider,2000,0,locationListener);
// 设置动态回调函数.statusListener是回调函数
lm.addGpsStatusListener(statusListener); // 注册状态信息回调.
}else if (v == btn2){
finish();
// super.onDestroy();
// super.onStop();
// lm.removeUpdates(locationListener);
}
}
// 卫星状态监听器
GpsStatus.Listener statusListener = new GpsStatus.Listener() {
@Override
public void onGpsStatusChanged(int i) {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
GpsStatus status = lm.getGpsStatus(null);
updateGpsStatus(i,status);
}
};
// 获取卫星数
private void updateGpsStatus(int event, GpsStatus status){
if (status == null){
}else if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS){
// 获取最大的卫星数(这只是一个预设的值)
int maxStaellites = status.getMaxSatellites();
Iterator<GpsSatellite> it = status.getSatellites().iterator();
numSatlliteList.clear();
int count = 0;
while(it.hasNext() && count <= maxStaellites){
GpsSatellite s = it.next();
numSatlliteList.add(s);
count++;
}
}else if(event == GpsStatus.GPS_EVENT_STARTED){
// 定位开始
}else if (event == GpsStatus.GPS_EVENT_STOPPED){
// 定位结束
}
}
// 定义更新显示的方法
private void updateShow(Location location){
if (location!=null){
tv_1.setText("经度:"+location.getLongitude());
tv_2.setText("维度:"+location.getLatitude());
tv_3.setText("海拔:"+location.getAltitude());
tv_4.setText("速度:"+location.getSpeed());
tv_5.setText("方位:"+location.getBearing());
tv_6.setText("时间:"+Timetool.SdateAllTime_mc());
tv_7.setText("卫星数:"+numSatlliteList.size());
}else
{
tv_1.setText("地理位置位置或正在获取地理位置中...");
}
}
private boolean isGpsAble(LocationManager lm) {
return lm.isProviderEnabled(LocationManager.GPS_PROVIDER)?true:false;
}
// 打开设置界面让用户自己设置
private void openGps(){
Intent intent = new Intent(Settings.ACTION_LOCALE_SETTINGS);
startActivityForResult(intent,0);
}
}
需要注意到的是:
- 我屡次报错的原因:
“从Android 6.0(API级别23)开始,用户在应用程序运行时向应用程序授予权限,而不是在安装应用程序时授予权限。” 在这种情况下,“ACCESS_FINE_LOCATION”是一个“危险权限,因此,你会得到这个'java.lang.SecurityException:”gps“位置提供者需要ACCESS_FINE_LOCATION权限。” 错误.
- 解决方法:
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
关于该问题的详细说明请参看:
在运行时请求权限
- 演示
- 这里需要注意的是:
如果你是在模拟器上测试程序时,请手动打开应用的权限设置,并给予程序获取定位信息的权限.否则模拟器是不会有提示的,你只会获得下面这样的一段崩溃记录...
就记录到这里了,关于gps定位服务的详细学习在之后会单独出笔记记录,今天是就照案列敲的一次练习,并简记一下从中学到的的一些东西.并深刻体会下这种坑爹的学习方式.
更新时间:
2019-4-10
1:36
[android学习]android_gps定位服务简单实现的更多相关文章
- Android开发--用户定位服务--UserLocation
Android开发--用户定位服务--UserLocation 2013-01-28 08:32:26 我来说两句 作者:BruceZhang 收藏 我要投稿 [java] & ...
- Android 学习笔记 Service服务与远程通信...(AIDL)
PS:这一章节看的我有几分迷茫,不是很容易理解...不过还好总算是明白了一大半了...基本的迷惑是解决了... 学习内容: 1.跨应用启动服务... 2.跨应用绑定服务... 3.跨应用实现通信... ...
- Android中GPS定位的简单应用
在Android中通过GPS获得当前位置,首先要获得一个LocationManager实例,通过该实例的getLastKnownLocation()方法获得第一个的位置,该方法的说明如下: void ...
- Android学习笔记:ListView简单应用--显示文字列表
在activity中的编写如下代码: final List<String> items = new ArrayList<String>(); //设置要显示的数据,这里因为是例 ...
- Android学习笔记_23_服务Service之AIDL和远程服务实现进程通信以及进程间传递自定义类型参数
一.了解AIDL语言: 在Android中, 每个应用程序都有自己的进程,当需要在不同的进程之间传递对象时,该如何实现呢? 显然, Java中是不支持跨进程内存共享的.因此要传递对象, 需要把对象解析 ...
- Android学习笔记_22_服务Service应用之—与Activity进行相互通信的本地服务
一.启动服务的两种方法方法: 第一种: startService()和stopService()启动关闭服务.适用于服务和Activity之间没有调用交互的情况.如果相互之间需要方法调用或者传递参数 ...
- Android学习笔记_46_fragment的简单应用
Fragments 诞生初衷 http://www.cnblogs.com/TerryBlog/archive/2012/02/17/2355753.html 自从Android 3.0中引入frag ...
- Android网络定位服务定制简述
Android 添加高德或百度网络定位服务 Android的网络定位服务以第三方的APK方式提供服务,由于在国内Android原生自带的com.google.android.gms服务几乎处于不可用状 ...
- iOS6定位服务编程详解
现在的移动设备很多都提供定位服务,使用iOS系统的iPhone.iPod Touch和iPad都可以提供位置服务,iOS设备能提供3种不同途径进行定位:Wifi, 蜂窝式移动电话基站, GPS卫星 i ...
随机推荐
- 386. Lexicographical Numbers 输出1到n之间按lexico排列的数字序列
[抄题]: Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,1 ...
- [leetcode]12. Integer to Roman整数转罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- Pycharm中选择Python解释器
新建项目后,有时候Pycharm找不到Python解释器,如果找不到的话,就会报错.报错信息: No python interpreter configured for the project 找到P ...
- ABP 权限拦截 第二篇
由于访问人数过多,我今天从新整理一下ABP权限认证机制,帮助大家更容易读懂 1.Abp 的权限拦截主要通过过滤器, public class AbpAuthorizationFilter : I ...
- C语言基础课第五次作业
PTA第五次作业 7-2 统计一行文本中的单词个数 一.程序代码 #include<stdio.h> int main(void) { ]; ,word=; char c; gets(st ...
- PB的一些记录
断点设置在函数内,发现返回值没有,需要取消函数内断点才正常 加密--采用矩阵乘法 行列式取+_1的矩阵与逆矩阵其元素都是整数,, 可以使用matlab来找到这些矩阵 A* I =E A ...
- s3 Docker的镜像和容器
Docker技术里最为基础的两大概念:镜像和容器.镜像的 获取方式:从registry拉取,从Dockerfile构建:容器的基本操作 1 Docker架构和底层技术简介 Docker Platfor ...
- FastDFS分布式文件系统配置文件详解
一.tracker配置文件详解: # is this config file disabled# false for enabled# true for disableddisabled=false# ...
- ZZ:git只clone仓库中指定子目录和指定文件的实现
原文链接: http://blog.csdn.net/xuyaqun/article/details/49275477 git目前唯一不能实现的是:不能像svn那样,针对子目录设置权限,这与git分布 ...
- 将Linux(ubuntu)安装到U盘上,实现即插即用
说明: 本教程是说明如何将ubuntu系统安装到U盘上(也就是把U盘当做电脑的硬盘),可以实现U盘插到任何电脑上都能够在实体机上运行ubuntu系统,而且所有的运行配置都能被保存,相当于随身携带的一个 ...