stopSelf() vs stopSelf(int) vs stopService(Intent)

AndroidService

Android Problem Overview


What's the difference in calling
stopSelf() , stopSelf(int) or stopService(new Intent(this,MyServiceClass.class))
inside onStartCommand() ?

for example if I start the same services twice this way:

...
Intent myIntent1 = new Intent(AndroidAlarmService.this, MyAlarmService.class);
myIntent1.putExtra("test", 1); 
Intent myIntent2 = new Intent(AndroidAlarmService.this, MyAlarmService.class);
myIntent2.putExtra("test", 2);
startService(myIntent1);
startService(myIntent2);
...

And implement onStartCommand in this way:

public int onStartCommand(Intent intent, int flags, int startId)
{
Toast.makeText(this, "onStartCommand called "+intent.getIntExtra("test", 0), Toast.LENGTH_LONG).show();
stopService(new Intent(this,MyAlarmService.class));
return START_NOT_STICKY;
}

I get exactly the same behaviour with the three methods, that is onDestroy will only be called after onStartCommand is executed twice.

Android Solutions


Solution 1 - Android

I hope this will help you:

A started service must manage its own lifecycle. That is, the system does not stop or destroy the service unless it must recover system memory and the service continues to run after onStartCommand() returns. So, the service must stop itself by calling stopSelf() or another component can stop it by calling stopService().

Once requested to stop with stopSelf() or stopService(), the system destroys the service as soon as possible.

However, if your service handles multiple requests to onStartCommand() concurrently, then you shouldn't stop the service when you're done processing a start request, because you might have since received a new start request (stopping at the end of the first request would terminate the second one). To avoid this problem, you can use stopSelf(int) to ensure that your request to stop the service is always based on the most recent start request.

That is, when you call stopSelf(int), you pass the ID of the start request (the startId delivered to onStartCommand()) to which your stop request corresponds. Then if the service received a new start request before you were able to call stopSelf(int), then the ID will not match and the service will not stop.

Solution 2 - Android

Here's a simplified description:

  • stopSelf() is used to always stop the current service.

  • stopSelf(int startId) is also used to stop the current service, but only if startId was the ID specified the last time the service was started.

  • stopService(Intent service) is used to stop services, but from outside the service to be stopped.

This is the reference page.

Solution 3 - Android

The core functionality of all 3 methods are same and that's why they are having similar names. There are very small differences among them.

  • public final void stopSelf()

Class : This belongs to android.app.Service class

Called From : This method is intended to get called from inside the service only.

Behavior : Service will get stopped. onDestroy() state called.

  • public final void stopSelf(int startId)

Class : This belongs to android.app.Service class

Called From : This method is intended to get called from inside the service only.

Behavior : There is a method from older version stopSelfResult(int startId). This method is newer version of that method. Service will get stopped only if the most recent time it was started was startId. onDestroy() state called.

May be you can find this method helpful only in a case where you started 2-3 instance of same service but you want to stop them in a order you received with startId list stored with you. Need to be very careful while using this method.

  • public abstract boolean stopService(Intent service)

Class : This belongs to android.content.Context class

Called From : This method is intended to get called from outside the service though you are allowed to call from inside.

Behavior : If the service is not running, nothing happens. Otherwise it is stopped. Note that calls to startService() are not counted -- this stops the service no matter how many times it was started. onDestroy() state called.

Solution 4 - Android

stopService() is called from the class from where the Service is started. stopSelf() is called within the running service class to stop the Service

Solution 5 - Android

> stopSelf()=Stop the service, if it was previously started. > > stopSelf(int startId)=Old version of stopSelfResult(int) that doesn't > return a result. > > stopSelfResult(int startId)=Stop the service if the most recent time > it was started was startId.Its return boolean result.

Solution 6 - Android

There are two flavors of stopSelf(). One takes a startId parameter, to indicate that you have completed work on one specific command, and so the service should opportunistically stop if there are no other outstanding commands.

Solution 7 - Android

stopSelf(int)- which is used to stop most recent start service based on onStartCommand(Intent i, int f, int startid)'s int startid.

stopSelf()- which is called by service itself , when task has been completed.

stopService(Intent) - which is explicitly called from an activity to stop the service.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGionJhView Question on Stackoverflow
Solution 1 - AndroidSourabhTechView Answer on Stackoverflow
Solution 2 - AndroidscotttView Answer on Stackoverflow
Solution 3 - AndroidMKJParekhView Answer on Stackoverflow
Solution 4 - Androidnawaab saabView Answer on Stackoverflow
Solution 5 - AndroidNeha - SystematixView Answer on Stackoverflow
Solution 6 - AndroidCommonsWareView Answer on Stackoverflow
Solution 7 - AndroidSekhar MadhiyazhaganView Answer on Stackoverflow