Android_GPS详解
来源:互联网 发布:端口2482 编辑:程序博客网 时间:2024/06/16 05:46
android 定位一般有四种方法,这四种方式分别是:GPS定位,WIFI定位,基站定位,AGPS定位。
本篇博文主要记录一下GPS定位:这种方式需要手机支持GPS模块硬件支持。通过GPS方式准确度是最高的,但是它的缺点也非常明显:
1、比较耗电;
2、绝大部分用户默认不开启GPS模块;
3、从GPS模块启动到获取第一次定位数据,可能需要比较长的时间;
4、室内几乎无法使用。
这其中,缺点2,3都是比较致命的。
GPS定位优点:GPS走的是卫星通信的通道,在没有网络连接的情况下也能使用。
代码详解如下:
public class MainActivity extends AppCompatActivity { private LocationManager lm; private EditText ed; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ed = (EditText) findViewById(R.id.ed); //获取位置服务管理器 lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // 判断GPS是否正常启动 if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) { Toast.makeText(this, "请开启GPS导航...", Toast.LENGTH_SHORT).show(); // 返回开启GPS导航设置界面 Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivityForResult(intent, 1); } // syst.addGpsStatusListener(gps); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } // 绑定监听,有4个参数 监听位置变化 // 参数1,设备:有GPS_PROVIDER和NETWORK_PROVIDER两种 // 参数2,位置信息更新周期,单位毫秒 // 参数3,位置变化最小距离:当位置距离变化超过此值时,将更新位置信息 // 参数4,监听 // 备注:参数2和3,如果参数3不为0,则以参数3为准;参数3为0,则通过时间来定时更新;两者为0,则随时刷新 // 1秒更新一次,或最小位移变化超过1米更新一次; // 注意:此处更新准确度非常低,推荐在service里面启动一个Thread,在run中sleep(10000);然后执行handler.sendMessage(),更新位置 lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, lo); } // 位置监听 LocationListener lo = new LocationListener() { /** * 位置信息变化时触发 */ //位置发生变换的时候调用该方法 @Override public void onLocationChanged(Location location) { updateView(location); } /** * GPS状态变化时触发 */ @Override public void onStatusChanged(String provider, int status, Bundle extras) { switch (status) { // GPS状态为可见时 case LocationProvider.AVAILABLE: Log.i("sxl", "当前GPS状态为可见状态"); break; // GPS状态为服务区外时 case LocationProvider.OUT_OF_SERVICE: Log.i("sxl", "当前GPS状态为服务区外状态"); break; // GPS状态为暂停服务时 case LocationProvider.TEMPORARILY_UNAVAILABLE: Log.i("sxl", "当前GPS状态为暂停服务状态"); break; } } /** * GPS开启时触发 */ @Override public void onProviderEnabled(String provider) { if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } Location location = lm.getLastKnownLocation(provider); updateView(location); } /** * GPS禁用时触发 */ @Override public void onProviderDisabled(String s) {// updateView(null); } }; /** * 实时更新文本内容 * * */ public void updateView(Location lo){ ed.setText("经度:"); ed.append(String.valueOf(lo.getLongitude())); ed.append("纬度:"); ed.append(String.valueOf(lo.getLatitude())); ed.append("海拔:"); ed.append(String.valueOf(lo.getAltitude())); }}
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="alice.bw.com.day10_gps.MainActivity"> <EditText android:id="@+id/ed" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>
AndroidMainifest中涉及的权限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.LOCATION_HARDWARE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.INTERNET"/>
阅读全文
0 0
- Android_GPS详解
- Android_GPS
- Android_gps
- Android_GPS
- (转)Android_GPS
- android_GPS UTC 时间到系统时间
- 详解
- 详解
- 详解
- 详解
- &,&&,|,||详解
- 详解
- Scala详解--------基础知识详解
- Spring详解-----------事务详解
- github 详解详解
- InputFilter详解、TextWatcher详解
- Spring详解-----------事务详解
- 【词汇详解】事务详解
- centos 7操作命令指南
- 大平台 or 小应用
- 计算1到n之间的所有数的平方和立方
- php实现协程,真正的异步
- 第十一周训练总结(二)
- Android_GPS详解
- Note of deep JVM(2)_hotspot JVM
- 错排。中的阶乘要注意不能超过20,不然longlong也救不了
- 设计模式-工厂模式
- 6.14
- 映客小视频怎么导出?映客小视频下载到本地的方法
- ButterKnife在RecyclerView adapter中的使用
- myeclipse基本的快捷键
- Github博客搭建