I'm working with a service which saves GPS latlng's continiously
But many times the service is getting restarted automatically, which results in data loss...
so is there any way that we can restrict the service from restarting. or any idea to clear this issue.
You can create a new process for the service by mentioning it in the Manifest file as
android:process=":newservice"
Inside the service tag
You can do couple of things.
1) detach any UI life-cycle components, preferably switch your service to a :remote process, using the manifest. This way if your activity crashes/gets killed, your service will remain unaffected because they were on different processes. Look into android IPC (simple way to communicate between your activity and this service when on different process is to use sockets on local host)
<service
android:name="myName.GPS"
android:enabled="true"
android:exported="false"
android:process=":remote">
</service>
2) make it a high priority foreground service (this will create a persistent notification)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//retrieve persisted data here
final Intent i = new Intent(this, ClassNameOfService.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
final NotificationCompat.Builder note =
new NotificationCompat.Builder(this)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.icon))
.setSmallIcon(R.drawable.icon)
.setContentTitle("GPS Running")
.setOngoing(true)
.setContentIntent(pi);
startForeground(10001, note.build()); //replace 10001 with whatever id you like
return super.onStartCommand(intent, flags, startId);
}
3) handle onLowMemory, onDestroy etc., gracefully so that you can restart your service. You can use shared prefs, or create an sql database to persist your data, depending on what suits your needs. Your service will get killed at one point or the other. So its better to have a persistence strategy.
@Override
public void onDestroy() {
//persist your data here
super.onDestroy();
}
But many times the service is getting restarted automatically, which results in data loss...
if the only problem you have is "data loss", then you can store whatever data you need in one of the persistent ways to do so. such as storing it with (SQLite database, SharedPreferences)
so is there any way that we can restrict the service from restarting.
assuming that you have good reason to do so, which I believe is not the case - there is a way to start service as a foreground service (it bundle the service with an ongoing undissmisable visible Notification
) when you start foreground service - the system gives much more priority to it - and will not kill it unless it really don't have any available memory at all to run the current foreground app..
also I have a felling that your location retrieving implementation is not the best approach - if your app needs to receive periodic location updates, then you should use Google Play location services . you can use this API's with a PendingIntent
callback that will wake up your service (and entire process if it stopped by the system) every time there is locaiton updates based on the parameters you provided to it. if you'll use this API's - you won't need to worry about cases that your Service wiil stop - because Google Play Services process will wake up your service every time it will notify you when there is new location..