To create a service, you must create a subclass of a service class (or a existing service subclass. In your implementation, You need to rewrite some callback methods that process key features of the service life cycle, and provide a suitable service binding mechanism for components. The important callback method to be rewritten is as follows:
Onstartcommand ()
When a component initiates a service by calling the startservice () method, the system calls the onstartcommand () method of the service. Once this method is executed, the service is started and runs indefinitely in the background. If you implement this method, you have the responsibility to terminate the service by calling the stopself () or stopservice () method when the service is finished. (If you only want the Service to provide the binding capability, you do not need to implement this method .)
Onbind ()
When a component wants to bindservice () method to bind to this service (such as executing RPC), the system will call this method. In the implementation of this method, you must return an ibinder object to provide an interface for users to interact with the service. You must implement this method, but if you do not allow binding, the method should return null.
Oncreate ()
When a service is created for the first time, the system will call this method to execute an installation process (it is called before the onstartcommand () or onbind () method ). If the service is running, this method will not be called.
Ondestroy ()
This method is called when the service is no longer in use or is being destroyed. Your service should use this method to clean up resources, such as clearing threads, registered listeners, and receivers. This is the final call acceptable to the service.
If a component starts a service by calling the startservice () method, the service will always run until it terminates the service by using the stopself () method, or another component calls the stopservice () method () method to terminate it.
If a component calls the bindservice () method to create the service (and does not call the onstartcommand () method), the service only runs for the same time as the bound component. Once this is unbound from all clients, the system will destroy it.
The Android system forces the termination of a service only when the memory is insufficient and the system resources must be recycled for the activity with the user focus. If a service is bound by an activity with a user focus, it cannot be killed. if the service is declared to run on the foreground (discussed later ), then it can hardly be killed. Otherwise, if the service is started and runs for a long time, the system will degrade its position in the background task list over time, and this service will be easily killed-if your service is started, you must design it to be able to be properly restarted through the system. If the system kills your service, it will restart once the resource becomes valid (although this also depends on the value returned from the onstartcommand () method, which will be discussed later ).