Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

START_STICKY_COMPATIBILITY in Services

Tags:

android

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?

like image 700
Diffy Avatar asked Sep 01 '16 19:09

Diffy


People also ask

What is Start_sticky?

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().

What is a sticky service?

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.

What is sticky and non sticky service?

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; }

What should onStartCommand () return incase if the service gets killed by OS?

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) .


1 Answers

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.

like image 132
csenga Avatar answered Oct 02 '22 20:10

csenga