Andriod in the life cycle of Service

2010-10-22  来源:本站原创  分类:Mobile  人气:132 

Andriod in the life cycle of Service

1. Service Life Cycle

1) by calling Context.startService () to start a Service, which may trigger the Service's onCreate () and onStart () operation, specifically the implementation of startService () will trigger onStart (), but if the Service has been system exists, the onCreate () will not be called again, only when you first start in the Service is triggered.

By Context.startService () to start Service will continue to run until the adoption Context.stopService () or stopSelf () to stop it. Many times through startService () start a service and does not generate multiple instances, but will lead to service onStart () was called multiple times, of course, as only one instance, so no matter how many times start, stop, just call it a Context . stopService () or stopSelf () on it.

2) You can also Context.bindService () to get a service link, the link is always to remain through a Context.unbindService () broken it. If the connection system, not the service, you may create a new service, when the Service's onCreate function also will be called. Service connection is established when the onBinder will be triggered, the connection is established through the onBinder can return after the IBinder interface object, use the service clients (such as an Activity) and Service by IBinder objects interact.

If a Service is through bindService () starts, it will always be there to not have any client connected to it so far, because there may be many clients connected to this service, then the client if a link is active Service will only be broken by a number of links, when reduced to 0 when the Service will be destroyed.

3) A Service can be activated either (start) can also be connected (bind), then Service life cycle depends on the way it is created, if it is through Context.startService () is created and the first case If by Context.bindService () using parameters Context.BIND_AUTO_CREATE created the situation and the second the same.

Of course, in Service to stop, is destroyed, it will trigger its onDestroy () function, we need to complete the Service related resources here to clean up, such as to stop the child thread, so the listener off.

2. Related to the official description (Android SDK1.5) as follows:

android-sdk documentation / docs / reference / android / app / Service.html

1) There are two reasons that a service can be run by the System. If someone calls Context.startService () then the System Will Retrieve the service (Creating and Calling its IT onCreate () method if needed) and then Call its onStart ( Intent, int) method with the arguments supplied by the client. The service at this Point Will continue running until Context.stopService () or stopSelf () is called. Note that multiple calls to Context.startService () do not Nest (though they do result in multiple corresponding calls to onStart ()), so no matter how many times it is started a service will be stopped once Context.stopService () or stopSelf () is called.

2) Clients can also use Context.bindService () to Obtain a Connection to a persistent service. This Likewise creates the service if IT is not already running (Calling onCreate () while doing SO), but does not Call onStart (). The Will the client receive IBinder Object that the service returns from its onBind (Intent) method, allowing the client to then make calls Back to the service. The service Will remain as long as running the Connection is established (whether or not the client retains a reference on the service's IBinder). Usually the IBinder returned is for a Complex interface that has been written in aidl .

3) A service can be bound both Started and have connections to IT. In such a case, the System Will keep the service running as long as IT is either Started or there are one or more connections to IT with the Context.BIND_AUTO_CREATE Flag. Once neither of these situations Hold, the service's onDestroy () method is called and the service is effectively terminated. All cleanup (Stopping threads, unregistering Receivers) should be Complete upon returning from onDestroy ().

Service life cycle of the following is timing diagram:

Andriod in the life cycle of Service

3. A few caveats

1) Service in any way create, are in the application's main thread is created, that is generated to create a Service that does not mean a new thread, Service creation process is blocking, and therefore need consider the performance, can not affect the interface and the logical follow-up operation.

2) If the Service does not generate its own new thread, that it is running in the application's main thread, so the Service itself does not improve application response time and other performance, but that generated by the back-office services to a new thread time-consuming operations such as dealing with large data read, etc. to improve response, Service itself and can not guarantee this. Service is equivalent to providing a platform for these time-consuming operation, which will create a new thread in the background to complete these tasks, and management of these threads as a variety of situations, including the destruction.

3) stopService and unbindService can put Service stopped, but stopped at once if you want a clear Service, use stopService more secure because unbindService Service is essentially the number of connections will be minus one, when reduced to 0 when they are destroyed The service instance, stopService the effect is equivalent to the number of connections immediately reduced to 0 to turn off the service, so the choice depends on the way off in different circumstances.