What to return from onStartCommand
Android #androidWhen a Service is started, onCreate
method is invoked (if it is not running already) and subsequently onStartCommand
is called.
From onStartCommand(...)
an int
value is returned which defines the restart behaviour of service. There are three values (ignoring
START_STICKY_COMPATIBILITY) for this purpose. I will discuss each of them briefly
- START_STICKY
- START_NOT_STICKY
- START_REDELIVER_INTENT
START_STICKY
When a START_STICKY
service is killed by system, it restarts with following arguments
-
If there is any pending intent to be delivered, it is passed to
onStartCommand
otherwisenull
is passed. -
If service is killed before completing
onStartCommand
, next time service if restarted withflags = Service.START_FLAG_RETRY
START_NOT_STICKY
In this case, service is only started if these is any pending intent to be delivered.
START_REDELIVER_INTENT
It is same as START_STICKY
with following differences
-
Service is restarted with last intent passed to service.
-
On restarting,
Service.START_FLAG_REDELIVERY
if passed toonStartCommand
flag.