Stopping a service
A started service must manage its own lifecycle. That was, 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 stopSelf()
to stop with or stopService()
, the system destroys the service as soon as possible.
However, if your service handles multiple requests onStartCommand()
to concurrently and then your shouldn ' t stop the service when you ' re Done processing a start request, because you might has since received a new start request (stopping at the end of the FIR St request would terminate the second one). To avoid this problem, you can use to ensure that your request to stop the service are always based on the most stopSelf(int)
re cent start request. That's, when you call stopSelf(int)
, you pass the ID of the The start request (the startId
delivered to) to onStartCommand()
which your stop reques T corresponds. Then if the service received a new start request before your were able to call stopSelf(int)
, then the ID would not match and the SE Rvice would not stop.
The above is a service documentation from Google's website, which basically means: first, the user must manage the service lifecycle, and secondly, when calling the Stopself (int) method, The Startid of the incoming Startid and Onstartcommand methods should correspond when the callback occurs, which ensures that when the service concurrently processes multiple requests, the second request is not completely executed because the service stops immediately after the first request ends. The red font above the stopself (int) method ends the service with the startid of the most recent request, that is, when the system is callback Stopself (Startid), Compares the incoming startid with the startid of the most recent request and, if the same, exits the service, otherwise the service will not be stopped.
For example, three arrows in the horizontal direction, representing three task requests for service, assuming they are executing the same time, further assuming that their startid are startId1, startId2, and StartId3, respectively. At T1 time, the service receives the first request, at T4 time the first request has been processed, this is called stopself (STARTID1), the end of the service life cycle, found the most recently requested Startid STARTID3 ( T3 time the third request arrived, so T4 time can not stop the service, the same T5 time also stop, until T6 time, three concurrent tasks are finished, only to successfully end the service life cycle.
Therefore, the basic conclusions are as follows:
- The Onstartcommand method of the same service can be invoked concurrently by the caller, meaning that the same service instance, whose Onstartcommand method may execute multiple request processing (inent) at the same time.
- The service's stopself (int) method is not necessarily able to end its life cycle when it is invoked.
Service stopself (int statid) and Onstartcommand (Intent intent,int flags,int Startid)