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