What is START_STICKY_COMPATIBILITY flag in terms of Android Services. The documentation mentions it
compatibility version of START_STICKY that does not guarantee that onStartCommand(Intent, int, int) will be called again after being killed.
What is compatibility version? And if its a version of START_STICKY
, then why is the call to onStartCommand()
is not guaranteed?And why will anyone use it when it doesn't guarantee that onStartCommand()
is ever called after the service is killed?
START_STICKY means the system will eventually restart your Service after it has been killed by the system. When it gets restarted, the Intent parameter to onStartCommand() will be null. This lets you detect when you get restarted as the Intent will otherwise be the one you passed to startService().
Sticky Services — Sticky service is somewhere between regular service and foreground service Android will kill the process time to time of this service, however it will be created automatically if the resources are available as soon as possible.
START_STICKY. the system will try to re-create your service after it is killed. START_NOT_STICKY. the system will not try to re-create your service after it is killed. Standard example: @Override public int onStartCommand(Intent intent, int flags, int startId) { return START_STICKY; }
Constant to return from onStartCommand(Intent, int, int) : if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int) ), then it will be scheduled for a restart and the last delivered Intent re-delivered to it again via onStartCommand(Intent, int, int) .
The default implementation of onStartCommand
:
public @StartResult int onStartCommand(Intent intent, @StartArgFlags int flags, int startId) {
onStart(intent, startId);
return mStartCompatibility ? START_STICKY_COMPATIBILITY : START_STICKY;
}
The mStartCompatibility
is determined this way:
mStartCompatibility = getApplicationInfo().targetSdkVersion
< Build.VERSION_CODES.ECLAIR;
In the 1.6 version of the Service
there is no implementation of onStartCommand
only onStart
.
In the version of 2.1 they made the onStart
deprecated.
Notice the difference in the parameters, flags
was introduced there.
By doing this, they will maintain compatibility with older system(PRE Eclair) which expects the old value and they also support the new behaviour in newer systems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With