-1

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:

  1. 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>
    
  2. 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?

Aggressor
  • 13,323
  • 24
  • 103
  • 182

1 Answers1

2

To clarify by restart I mean when I rebuild my apk to the phone.

If you uninstall, upgrade, or reinstall the app, all processes associated with that app will be terminated. For at least the upgrade scenario, you may be able register a receiver in the manifest for ACTION_PACKAGE_REPLACED to find out and restart your service, though I have not tried it.

Is the service not able to run independently of an apk re-install?

Only if it is not in that APK, but rather is a separate app in a separate APK.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Gotcha thank you. I think this will make my testing harder but once the code is working it should be fine. – Aggressor Dec 04 '15 at 22:44
  • This [solution](http://stackoverflow.com/questions/17768932/service-crashing-and-restarting#18199749) works for me as expected. – Sabari Karthik Dec 22 '16 at 05:52