|
在Android中处理GPS数据的时候,由于经验不丰富,容易出现如下问题,如更新太频繁,返回的Location无数据导致异常,数据不准确等等问题。在实际的项目中,我们也碰到过这些问题,从网上觅得此段代码,并经过细微的修改,应用到我们的GPS项目中,目前没有任何bug。不敢独享,特分享如下。在使用的时候,本代码部分放在一个Service中启动,代码如下:- public void startService()
- {
- locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
- locationListener = new GPSServiceListener(CONST.LOCATION_UPDATE_PERIOD_MSEC,true);
- Location loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
- locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, locationListener);
- }
-
- public void endService()
- {
- if(locationManager != null && locationListener != null)
- {
- locationManager.removeUpdates(locationListener);
- }
-
- }
复制代码 参考代码:
LocationManager calling onLocationChanged too often?
|
|