life cycle of 1.Service
The Service (services) in Android is different from the activity, it is unable to interact with the user, can not start their own, running in the background of the program, if we quit the application, the service process does not end, it is still running in the background, So when do we use the service? For example, when we play music, we may want to listen to music while doing something else, when we quit the Music play app, if we do not use the service, we can not hear the music, so this time we need to use the service, such as when we have an application of data through the network. Data at different times (over a period of time) is different. This time we can use the service to update the background regularly, without having to get it every time we open the app.
The life cycle of Android service is not as complex as activity, it just inherits OnCreate (), OnStart (), OnDestroy () Three methods, when we first start the service , The OnCreate () and OnStart () methods are called successively , and when the service is stopped, we execute the OnDestroy () method .
It is important to note that if the service is already started, when we start the service again , the OnCreate () method is not executed again, but the OnStart () method is executed directly.
2.startservice and Bindservice detailed
service life cycle method is less than activity, only OnCreate, OnStart, OnDestroy. We have two ways to start a service, and they have a different effect on the service life cycle.
(1) by StartService
Service will experience OnCreate--and OnStart, Direct OnDestroy when StopService.
If the caller exits without calling StopService, the service will always run in the background .
The next caller can still get up and stopservice.
(2) by Bindservice
The service will only run OnCreate, when the caller and service are bound together. when the caller exits , Srevice calls Onunbind-->ondestroyed, and the so-called binding together is a common survival .
Note: The OnCreate method of the service will only be called once ,
Is that you no matter how many times the StartService and Bindservice,service only be created once .
If bind is first , then start will run the service's OnStart method directly ,
If first start, then bind will run the Onbind method directly . If you bind it first, stop it .
can only first unbindservice, then StopService, so is the first start or bind behavior is a difference.
(3) Summary:
Services in Android and services in Windows are similar, services generally do not have user interface, it runs in the system is not easy to be detected by the user, you can use it to develop such as monitoring programs. The service cannot run itself and needs to start the service by calling the Context.startservice () or Context.bindservice () method .
Both of these methods can start the service, but they are used in different situations:
using the StartService () method to enable the service, there is no connection between the caller and the service . The service is still running even if the caller exits .
Using the Bindservice () method to enable the service, the caller and the service are bound together, and once the caller exits , the service terminates , and there is a "no need to live at the same time Must Die" feature .
If you intend to start the service with the Context.startservice () method, the system calls the service's OnCreate () method first when the service is not created .
then call the OnStart () method . If the StartService () method is called before the service has been created , calling the StartService () method multiple times does not cause the service to be created more than once.
However, it causes multiple calls to the OnStart () method . Services started with the StartService () method can only call the Context.stopservice () method to end the service , and the OnDestroy () method is called at the end of the service.
If you intend to start the service with the Context.bindservice () method, the system calls the service's OnCreate () method first when the service is not created .
then call the Onbind () method. This time the caller and the service are bound together , the caller exits, and the system calls the service's Onunbind () method first .
then call the OnDestroy () method .
If the service has been bound before the Bindservice () method is called,
Calling the Bindservice () method multiple times does not cause the service and bindings to be created multiple times (that is, the OnCreate () and Onbind () methods are not called more than once ).
If the caller wants to unbind the service being bound, it can call the Unbindservice () method, and calling the method will also cause the system to invoke the service's Onunbind ()-->ondestroy () method.
Android (Java) Learning Note 171:service life cycle