Reputation: 417
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);
}
Upvotes: 1
Views: 4605
Reputation: 417
SO far I am doing this.
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
CommonConstants.GPS_POLL,CommonConstants.GPS_MIN_DIST, pendingIntent);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
CommonConstants.NW_POLL, CommonConstants.NW_MIN_DIST, pendingIntent);
Please suggest if you have some better solution.
Upvotes: 0
Reputation: 1533
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
Upvotes: 1
Reputation: 29199
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.
Upvotes: 1