I have to make sure that my Service
will run until I'll explicitly call stopService()
.
so I should return from onStartCommand()
the START_STICKY
flag.
also I want that in case my Service will be destroyed by the system because low memory, when the system will re-create it - the onStartCommand() will be called with the same intent originally started the service in the first place.
because this reason - I would like to use the START_FLAG_REDELIVERY
flag.
what I described surely can happened (and actually happened to me ones) according to the documentation - http://developer.android.com/reference/android/app/Service.html#ProcessLifecycle
I know I can start the service with startForground()
, that prevents the service been candidate for killed by the system when the memory is low, but I prefer not to use this approach, and anyway - according to the documentation: the system still can kill it under extreme low memory pressure.
so it would be perfect if I could return from onStartCommand()
START_STICKY|START_FLAG_REDELIVERY
but according to the documentation it seems like you can use only one of the flags (maybe I'm wrong??).
so is it possible to start somehow service that will work in such a way as I described?
You cant return multiple values. Instead, I would suggest you to return START_STICKY
flag in onStartCommand
. But for the first time, save the necessary values of intent's bundle in a SharedPreference
storage. Next time, when your onStartCommand
is re-called (after recovering from low memory or whatever reason) and the intent is found to be null, then simply load the values from preferences and keep on going with what ever you want.