"If you start the android service using startService(..) , this service will work until you explicitly call stopService(..) . There are two reasons why the system can manage the service. If someone calls Context.startService() , then the system will receive the service (creating it and calling its onCreate() method, if necessary), and then calling its onStartCommand(Intent, int, int) method with the arguments provided by the client. The service will continue to work until until Context.stopService() or stopSelf() 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 be stopped after calling Context.stopService() or stopSelf() ; however, services can use their stopSelf(int) method stopSelf(int) to ensure that the service is not stopped until the started intentions are processed.
Clients can also use Context.bindService() to get a permanent connection to the service. This also creates the service if it is not already running (calling onCreate() at the same time), but does not call onStartCommand() . The client will receive an IBinder object that the service returns from its onBind(Intent) method, allowing the client to make callbacks to the service. The service will work until the connection is established (regardless of whether the client keeps a link to the IBinder Service). The usually returned IBinder is a sophisticated interface written in AIDL.
A service can be started and associated with it. In this case, the system will keep the service running as long as it is running or there is one or more connections to it with the Context.BIND_AUTO_CREATE flag. When none of these situations occurs, the Service onDestroy() method is called and the service actually terminates. All cleaning (stopping threads, unregistered receivers) should be completed after returning from onDestroy() .
Schildmeijer Dec 16 '09 at 18:27 2009-12-16 18:27
source share