我已创建GPS
服务,该服务定期读取位置更新。此前有一段时间GPS
正在进入睡眠模式,并且之后没有位置更新。GPS正在安卓服务的睡眠模式下运行
现在我已更改我的代码以定期取消注册并注册GPS
位置更新。这解决了我的GPS
进入睡眠模式的问题,但创建了一个新问题。那是
我的服务在一段时间后终止。我无法检查原因。请帮忙。
这里有一些代码snipplets
@Override
public void onCreate() {
super.onCreate();
timer.schedule(checkLocationListener, new Date(), 1000 * 60 * 15);
}
private TimerTask checkLocationListener = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
lm.removeUpdates(LocationService.this);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, CommonConstants.GPS_POLL,
CommonConstants.GPS_MIN_DIST, LocationService.this);
}
});
}
};
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(LocationService.class.getSimpleName(), "Start");
valueMap.put(CommonConstants.STATUS, CommonConstants.STATUS_DROP);
trackMe();
new Thread() {
public void run() {
while (true) {
try {
String url = DeviceConfig.getMessage();
if (url == null) {
Thread.sleep(60000);
continue;
}
HttpRequester.getServerResponse(url, null);
DeviceConfig.addValue(CommonConstants.SERVER_UPD, "" + System.currentTimeMillis());
DeviceConfig.removeMessage();
} catch (Exception e) {
Log.e(LocationService.class.getSimpleName(), "Error", e);
}
}
}
}.start();
return (START_NOT_STICKY);
}
private void trackMe() {
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, CommonConstants.GPS_POLL, CommonConstants.GPS_MIN_DIST,
this);
DeviceConfig.addValue(CommonConstants.GPS_STATE, lm.isProviderEnabled(LocationManager.GPS_PROVIDER)
? CommonConstants.ON : CommonConstants.OFF);
}
嗨pramod,有没有为你工作? – viv 2013-10-08 10:50:24
即使按下主页按钮时,我仍然遇到此问题......... – viv 2013-10-08 12:21:32