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.
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)
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..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)
2) make it a high priority foreground service (this will create a persistent notification)
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.
You can create a new process for the service by mentioning it in the Manifest file as
Inside the service tag