Allaudin's Blog All about Android

What to return from onStartCommand

When a Service is started, onCreate method is invoked (if it is not running already) and subsequently onStartCommand is called.

void onCreate();
int onStartCommand(Intent intent, int flags, int startId);

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



When a START_STICKY service is killed by system, it restarts with following arguments

  1. If there is any pending intent to be delivered, it is passed to onStartCommand otherwise null is passed.

  2. If service is killed before completing onStartCommand, next time service if restarted with flags = Service.START_FLAG_RETRY


In this case, service is only started if these is any pending intent to be delivered.


It is same as START_STICKY with following differences

  1. Service is restarted with last intent passed to service.

  2. On restarting, Service.START_FLAG_REDELIVERY if passed to onStartCommand flag.