Android 服务中的 GPS 进入睡眠模式
我创建了一个 GPS
服务,它定期读取位置更新。早些时候存在一个问题,GPS
一段时间后进入睡眠模式,此后没有提供位置更新。
现在,我已更改代码以定期取消注册和注册 GPS
位置更新。这解决了我的 GPS
进入睡眠模式的问题,但又产生了一个新问题。那是
我的服务在一段时间后终止。我无法检查其原因。请帮忙。
这是一些代码片段
@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);
}
I have created a GPS
service which reads location updates on a regular interval. Earlier there was an issue that GPS
was going in sleep mode after some time and no location updates were giver thereafter.
Now I have changed my code to unregister and register GPS
location updates on a regular interval. This solves my problem of GPS
going in sleep mode but a new issue is created. That is
My service is terminated after some time. I am not able to check the reason for this. Please Help.
Here are some code 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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不需要一次又一次地注册/取消注册位置更新侦听器,看起来您的服务已停止,因此以粘性模式启动您的服务,这样,只能显式停止服务。
You wont need to register/unregister location update listener again and again, It seems your service stopped, so start your service in sticky mode, in that way, service can be stopped explicitly only.
如果您确实希望服务继续运行,则需要将其设为前台服务(请参阅 http://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification))
但为什么不使用这个 http://developer.android.com/reference/android/location/LocationManager .html#requestLocationUpdates(long, float, android.location.Criteria, android.app.PendingIntent) 每次位置更改时启动您的服务
Phil ?
If you really want your service to keep running you need to make it a foreground service (see http://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification))
But why not use this http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(long, float, android.location.Criteria, android.app.PendingIntent) to start your service each time the location changes?
Phil
到目前为止我正在这样做。
请建议您是否有更好的解决方案。
SO far I am doing this.
Please suggest if you have some better solution.