• 前言

    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定位服务简单实现的更多相关文章

  1. Android开发--用户定位服务--UserLocation

    Android开发--用户定位服务--UserLocation 2013-01-28 08:32:26     我来说两句      作者:BruceZhang 收藏    我要投稿 [java] & ...

  2. Android 学习笔记 Service服务与远程通信...(AIDL)

    PS:这一章节看的我有几分迷茫,不是很容易理解...不过还好总算是明白了一大半了...基本的迷惑是解决了... 学习内容: 1.跨应用启动服务... 2.跨应用绑定服务... 3.跨应用实现通信... ...

  3. Android中GPS定位的简单应用

    在Android中通过GPS获得当前位置,首先要获得一个LocationManager实例,通过该实例的getLastKnownLocation()方法获得第一个的位置,该方法的说明如下: void ...

  4. Android学习笔记:ListView简单应用--显示文字列表

    在activity中的编写如下代码: final List<String> items = new ArrayList<String>(); //设置要显示的数据,这里因为是例 ...

  5. Android学习笔记_23_服务Service之AIDL和远程服务实现进程通信以及进程间传递自定义类型参数

    一.了解AIDL语言: 在Android中, 每个应用程序都有自己的进程,当需要在不同的进程之间传递对象时,该如何实现呢? 显然, Java中是不支持跨进程内存共享的.因此要传递对象, 需要把对象解析 ...

  6. Android学习笔记_22_服务Service应用之—与Activity进行相互通信的本地服务

    一.启动服务的两种方法方法: 第一种:  startService()和stopService()启动关闭服务.适用于服务和Activity之间没有调用交互的情况.如果相互之间需要方法调用或者传递参数 ...

  7. Android学习笔记_46_fragment的简单应用

    Fragments 诞生初衷 http://www.cnblogs.com/TerryBlog/archive/2012/02/17/2355753.html 自从Android 3.0中引入frag ...

  8. Android网络定位服务定制简述

    Android 添加高德或百度网络定位服务 Android的网络定位服务以第三方的APK方式提供服务,由于在国内Android原生自带的com.google.android.gms服务几乎处于不可用状 ...

  9. iOS6定位服务编程详解

    现在的移动设备很多都提供定位服务,使用iOS系统的iPhone.iPod Touch和iPad都可以提供位置服务,iOS设备能提供3种不同途径进行定位:Wifi, 蜂窝式移动电话基站, GPS卫星 i ...

随机推荐

  1. js数组去除重复数据

    一个有重复数据的数组,准备一个空数组,遍历有重复数据的数组同时用indexOf对比那个空数组判断是否有一样的,不一样的push进去空数组 let arr = dataInfo.map(item =&g ...

  2. git回滚远程仓库

    关于远程仓库回滚 首先,必须要明白的一件事,任何普通用户不能擅自做有关远程仓库回退的操作,如果你擅自回滚了远程仓库,会对项目团队其他人造成不可预知的影响.如果需要回退版本,先联系项目的仓库管理员,在团 ...

  3. Android 面试问答

    Android 面试问答 目录 数据结构和算法 java核心知识 Android核心知识 架构 设计相关问题 相关工具和技术 Android 测试驱动开发 其他 数据结构和算法 ******关于此类问 ...

  4. Vmware unknow Interface ens33

    vmare打开虚拟网络编辑器,按图示操作

  5. Linux驱动之同步、互斥、阻塞的应用

    同步.互斥.阻塞的概念: 同步:在并发程序设计中,各进程对公共变量的访问必须加以制约,这种制约称为同步. 互斥机制:访问共享资源的代码区叫做临界区,这里的共享资源可能被多个线程需要,但这些共享资源又不 ...

  6. PHP开发——分支结构

    If if if···else··· if···else··· switch

  7. CentOS_mini下安装docker 之 yum mount

    --->linux 终端输出太多前面看不到的解决办法:shift+page up --->mount命令[-参数] [设备名称] [挂载点] mkdir /mnt/CentOS mount ...

  8. 主机性能监控之wmi 获取系统信息及内存性能信息

    标 题: 主机性能监控之wmi 获取系统信息及内存性能信息作 者: itdef链 接: http://www.cnblogs.com/itdef/p/3990240.html 欢迎转帖 请保持文本完整 ...

  9. python_day1_数据类型

    数据类型: python基本数据类型有:int(整型),str(字符串),list[](列表),dict{}(字典),li()(元祖)bool(布尔) 注:还有一个Long(长整型),python3里 ...

  10. java_io

    JAVA IO流(一)参考文章:http://ifeve.com/java-io-network/,并发编程网原创文章,转载请注明: 转载自并发编程网 – ifeve.com本文链接地址: Java ...