我正在编写一项服务,每30分钟记录一次您的GPS位置。我应该写什么来打开位置监听器,等待x分钟直到我的location.accuracy小于25米,然后关闭位置监听器。
现在我的方法是耗尽太多电池而且不能接受。
谢谢
答案 0 :(得分:0)
我可能会为您提供一段简单的代码。 当您启动GPS时,您只需定义时间和距离限制。 另外,请不要忘记在退出应用程序时终止此操作(下面的最后一个方法)
private static LocationListener gpsLocationListener = null;
/* GPS will not detach and update every 30 minutes if the delta > 25 meters */
private void getGPSLocation(Context context){
gpsLocationListener = new GPSLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1800000, 25, gpsLocationListener);
}
private class GPSLocationListener implements LocationListener{
public void onLocationChanged(Location location){
if(location != null){
final double lat = location.getLatitude();
final double lng = location.getLongitude();
// can do your other implementation here //
}
}
@Override
public void onProviderDisabled(String provider){ }
@Override
public void onProviderEnabled(String provider){ }
@Override
public void onStatusChanged(String provider, int status, Bundle extras){ }
}
private void killGPS(){
if(locationManager != null && gpsLocationListener != null){
locationManager.removeUpdates(gpsLocationListener); }
}
}
希望这有帮助
-serkan