初始化:

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // 位置if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {// TODO: Open GPS} else {String bestProvider = locationManager.getBestProvider(getLocationCriteria(), true);// 获取位置信息// 如果不设置查询要求,getLastKnownLocation方法传人的参数为LocationManager.GPS_PROVIDERLocation location = locationManager.getLastKnownLocation(bestProvider);// 监听状态locationManager.addGpsStatusListener(gpsStatusListener);// 绑定监听,有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(),更新位置locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locationListener);}


onDestroy时remove监听:

@Overrideprotected void onDestroy() {if (locationManager != null) {locationManager.removeGpsStatusListener(gpsStatusListener);}super.onDestroy();}


位置监听:

// 位置监听private LocationListener locationListener = new LocationListener() {/** * 位置信息变化时触发 */public void onLocationChanged(Location location) {// location.getAltitude(); -- 海拔updateSpeedByLocation(location);}/** * GPS状态变化时触发 */public void onStatusChanged(String provider, int status, Bundle extras) {switch (status) {case LocationProvider.AVAILABLE: // GPS状态为可见时MyLog.i("GPS", "当前GPS状态为可见状态");break;case LocationProvider.OUT_OF_SERVICE: // GPS状态为服务区外时MyLog.i("GPS", "当前GPS状态为服务区外状态");break;case LocationProvider.TEMPORARILY_UNAVAILABLE: // GPS状态为暂停服务时MyLog.i("GPS", "当前GPS状态为暂停服务状态");break;}}/** * GPS开启时触发 */public void onProviderEnabled(String provider) {Location location = locationManager.getLastKnownLocation(provider);updateSpeedByLocation(location);}/** * GPS禁用时触发 */public void onProviderDisabled(String provider) {// updateView(null);}};


获取查询条件:

/** * 返回查询条件 *  * @return */private Criteria getLocationCriteria() {Criteria criteria = new Criteria();// 设置定位精确度 Criteria.ACCURACY_COARSE比较粗略,Criteria.ACCURACY_FINE则比较精细criteria.setAccuracy(Criteria.ACCURACY_FINE);criteria.setSpeedRequired(true); // 设置是否要求速度criteria.setCostAllowed(false); // 设置是否允许运营商收费criteria.setBearingRequired(false); // 设置是否需要方位信息criteria.setAltitudeRequired(false); // 设置是否需要海拔信息criteria.setPowerRequirement(Criteria.POWER_LOW); // 设置对电源的需求return criteria;}


GPS状态监听

// 状态监听GpsStatus.Listener gpsStatusListener = new GpsStatus.Listener() {public void onGpsStatusChanged(int event) {switch (event) {case GpsStatus.GPS_EVENT_FIRST_FIX: // 第一次定位MyLog.i("GPS", "GPS_EVENT_FIRST_FIX");break;case GpsStatus.GPS_EVENT_SATELLITE_STATUS: // 卫星状态改变GpsStatus gpsStatus = locationManager.getGpsStatus(null); // 获取当前状态int maxSatellites = gpsStatus.getMaxSatellites(); // 获取卫星颗数的默认最大值Iterator iters = gpsStatus.getSatellites().iterator(); // 创建一个迭代器保存所有卫星int count = 0;while (iters.hasNext() && count <= maxSatellites) {GpsSatellite s = iters.next();count++;}MyLog.i("GPS", "Satellite Number:" + count);break;case GpsStatus.GPS_EVENT_STARTED: // 定位启动MyLog.i("GPS", "GPS_EVENT_STARTED");break;case GpsStatus.GPS_EVENT_STOPPED: // 定位结束MyLog.i("GPS", "GPS_EVENT_STOPPED");break;}};};


根据Location获取速度

private void updateSpeedByLocation(Location location) {int tempSpeed = (int) (location.getSpeed() * 3.6); // m/s --> Km/hadasSpeed = tempSpeed;recordSpeed = tempSpeed;nowLatitude = location.getLatitude();nowLongitude = location.getLongitude();MyLog.i("GPS", "Speed:" + tempSpeed);if (recorderFront != null) {if (recordSpeed > 0) {recorderFront.setSpeed(recordSpeed);recordSpeed = 0; // 清除速度}recorderFront.setLat(new DecimalFormat("#.00000").format(nowLatitude) + "");recorderFront.setLong(new DecimalFormat("#.00000").format(nowLongitude) + "");}}


更多相关文章

  1. Android(安卓)Training - 使用IntentService执行任务(Lesson 3 -
  2. Android(安卓)动态隐藏显示导航栏,状态栏
  3. Android(安卓)wifi信号强弱检测
  4. android 6.0锁屏界面时间位置修改
  5. the android sdk folder can no longer be inside the applicati
  6. Android(安卓)Q 版本新的网络状况判断方式
  7. Android(安卓)手机状态
  8. android滑动删除的一个开源项目SwipeDelMenuLayout的简单使用
  9. Android实现文本的展开收起

随机推荐

  1. Android(安卓)占位符 %1$s %1$d
  2. Android(安卓)ApiDemo(十二)-- Graphics2
  3. Deepin/Ubuntu/Ubuntukylin/Xubunt/Lubun
  4. android之wifi移植全过程
  5. Android(安卓)打包, 不同渠道使用 不同的
  6. Android23_网络存储之HttpClient
  7. MPAndroidChart项目实战——MarkerView显
  8. android在armv4t中跑
  9. android代码中打开系统设置界面
  10. Android——使用WebView显示网页