I have read about a dozen posts on how to setup an independent Service.
As a result of my research and testing I have done the following:
Created a process tag on my service so my Service should run in a separate thread:
<service android:name=".Services.S_GPS" android:process="sgps"></service>
Set the onStartCommand to return START_NOT_STICKY and start the service in the foreground:
-
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Notification notification = new Notification();
startForeground(1337,notification);
return START_NOT_STICKY;
}
Even after all this when I terminate or restart my application, the service restarts:
W/ActivityManager: Scheduling restart of crashed service co.myapp.app/.Services.S_GPS in 1000ms
My question is what else am I missing to prevent my Service from shutting down when the app restarts? I suspect I've still done something wrong and the service is running in the app's thread. I need my service to run 100% independent from the app, I don't need them to communicate (other than the App starting it).
Can you advise how I can prevent my service from crashing when I close or restart the app?
Update To clarify by restart I mean when I rebuild my apk to the phone. Once I start my service, I comment out the code thats starting the service up. When I re-install my apk it kills the service (even though there is no code re-creating it). Is the service not able to run independently of an apk re-install?