If you start the service using startService (), you must stop it using stopService () .
There are two reasons why a system can manage a service. If someone calls Context.startService (), then the system will receive the service (by creating it and calling its onCreate () method, if necessary), and then calling its onStartCommand (Intent, int, int) method with arguments provided by the client . The service at this point will continue to work until Context.stopService () or stopSelf () is called. Note that several calls to Context.startService () are not nested (although they lead to several corresponding calls to onStartCommand ()), so no matter how many times it starts, the service will stop after Context.stopService () or stopSelf ( ) is called; however, services can use the stopSelf (int) method to ensure that the service will not be stopped until the started intentions are processed.
You can associate as many ServiceConnection as you like with bindService () , but pay attention to the flag that you passed to it. If you pass 0, then if you call stopService (), the service will stop (I don't know what the ServiceConnection will happen to you). Otherwise, if you want your service to be active as long as the ServiceConnection is not tied to it, use BIND_AUTO_CREATE .
this is from stopService ():
Request that this application service be stopped. If the service does not work, nothing happens. Otherwise, it will be stopped. Note that calls to startService () are not taken into account - this stops the service no matter how many times it has been started.
Note that if a stopped service still has ServiceConnection objects bound to it using BIND_AUTO_CREATE, it will not be destroyed until all these bindings are deleted. See the service documentation for more information on the service life cycle.
This function will raise a SecurityException if you do not have permission to stop this service.
Hope this helps.
hara Aug 18 '10 at 16:44 2010-08-18 16:44
source share