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--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
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.
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.
Onbind will return a Ibind interface instance to the client, Ibind allow the client to callback the service's methods, such as getting service running status or other operations. At this point the caller is bound to the service, but Onbind can only be used once and cannot be bound more than once.
Only OnStart can be called multiple times (through multiple StartService calls) during each turn-off of the service, and the other oncreate,onbind,onunbind,ondestory can only be called once in a life cycle.
Since the service in Android uses the Onbind method to bind services, return a IBinder object to operate, and we want to get the content of the specific service method, we need to IBinder object to return to the specific service object in order to operate , so that the specific service object must first implement the Binder object, this way we can use the Bindservice method to bind the service, obtain the binder object after the specific service object, Then get the methods in the service and so on.
Life cycle methods related to starting services with the Context.startservice () method
OnCreate ()--onstart ()--ondestroy ()
OnCreate () This method is called when the service is created, and the method is only called once, and the service is created only once, regardless of how many times the StartService () or Bindservice () method is called.
OnStart () The method is only invoked when the service is started with the Context.startservice () method. This method is called when the service starts running. The StartService () method is called multiple times although the service is not created more than once, but the OnStart () method is called multiple times.
OnDestroy () This method is called when the service is terminated.
Life cycle methods related to starting services with the Context.bindservice () method
OnCreate ()--onbind ()--onunbind ()--OnDestroy ()
Onbind () The method is only invoked when the service is started with the Context.bindservice () method. This method is called when the caller and the service are bound, and the Context.bindservice () method is called multiple times when the caller is tied to the service and does not cause the method to be called more than once.
[The difference between Androidtips]startservice and Bindservice