samedi 16 mai 2015

Starting a service with START_NOT_STICKY from application class not working

I have come across this strange issue. I want my service to be started once and not to be restarted if the system needs to release memory or if the application is killed. So in the onStartCommand() method I return START_NOT_STICKY.
At the beginning I called the startService()method in during the main activity onCreate(), passing the activity as the context. Everything was fine, if the app was killed the service was not restarted.
Then I realize it would be a more cleaner design if the application class calls startService()during its onCreate()so that the activity is no longer responsible of starting the service everytime the activity is recreated. So I moved the method to the application class and passing the application class object as the context.
Now the service is restarting everytime the process is killed. Somebody can please technically explain why is this happening? Thank you very much.

Aucun commentaire:

Enregistrer un commentaire