Service.onStartCommand () - is it possible to return more than one flag?

I have to make sure that my Service will work until I explicitly call stopService() . so I have to return from onStartCommand() flag START_STICKY .

I also want that in case my Service is destroyed by the system, because low memory when the system recreates it, onStartCommand () will be called with the same intention, which initially launched the service in the first place. because this reason - I would like to use the flag START_FLAG_REDELIVERY . what I described may have happened (and actually happened to me) according to the documentation - http://developer.android.com/reference/android/app/Service.html#ProcessLifecycle

I know that I can start the service using startForground() , which prevents the service from being a candidate for destruction by the system when memory is low, but I prefer not to use this approach, and in any case, according to the documentation: the system can still kill it when extremely low pressure in memory.

so it would be ideal if I could return from onStartCommand() START_STICKY|START_FLAG_REDELIVERY

but according to the documentation it seems that you can use only one of the flags (maybe I'm wrong ??).

is it possible to somehow start a service that will work as I described?

+4
source share
2 answers

You cannot return multiple values. Instead, I suggest you return the START_STICKY flag to onStartCommand . But for the first time, save the necessary intent set values ​​in the SharedPreference repository. The next time your onStartCommand is called again (after recovery due to low memory or for some reason), and it is assumed that this value is null, just load the values ​​from the settings and continue with what you want.

+4
source

the difference between START_STICKY and START_FLAG_REDELIVERY , as far as I remember, will simply repeat with zero intent or the same intent. It is just how you want to program it. You should return only one flag, because they have different modes of operation.

+1
source

Source: https://habr.com/ru/post/1439346/


All Articles