1. Steps
Step 1: Inherit the service class
Public class smsservice extends Service {}
Step 2: configure the service on the <Application> node in the androidmanifest. xml file:
<Service android: Name = ". demoservice"/>
Ii. Context. startservice () and context. bindservice
The service cannot run on its own. You must call the context. startservice () or context. bindservice () method to start the service. Both methods are available.
To start the service, but their usage is different.
1. Use the startservice () method to enable the service. There is no relation between the caller and the service. Even if the caller exits, the Service continues to run.
The bindservice () method is used to enable the Service. When the caller and the service are bound together, the service is terminated once the caller exits.
2. Start the service using the context. startservice () method. when the service is not created, the system first calls the oncreate () method of the service,
Call the onstart () method. If the service has been created before the startservice () method is called, multiple call of the startservice () method will not lead to multiple creation of the service, but will lead to multiple calls of the onstart () method.
A service started using the startservice () method can only end the service by calling the context. stopservice () method. The ondestroy () method is called when the service ends.
3. Start the service using the context. bindservice () method. when the service is not created, the system will first call the oncreate () method of the service,
Call the onbind () method. At this time, the caller and the service are bound together. After the caller exits, the system will first call the onunbind () method of the service and then call the ondestroy () method. If the service has been bound before the bindservice () method is called, multiple calls to the bindservice () method will not result in multiple service creation and binding (that is, oncreate () and onbind () method is not called multiple times ). If the caller wants to unbind from the service being bound, he can call the unbindservice () method. Calling this method also causes the system to call the onunbind () --> ondestroy () method of the service.
1. Common lifecycle callback methods for services are as follows:
Oncreate () This method is called when a service is created. This method is called only once, no matter how many times the startservice () or bindservice () method is called,
The Service is also created only once. Ondestroy () is called when the service is terminated.
2. Context. startservice () method for starting service-related lifecycles
Onstart () calls back this method only when the context. startservice () method is used to start the service. This method is called when the service starts running.
Although the startservice () method is called multiple times, the onstart () method is called multiple times.
3. Context. bindservice () method for starting the service Lifecycle
Onbind () calls back this method only when the context. bindservice () method is used to start the service. This method is called when the caller binds to the service,
When the caller and the service have been bound, multiple calls to the context. bindservice () method will not cause this method to be called multiple times.
Onunbind () calls back this method only when the context. bindservice () method is used to start the service. This method is called when the caller and the service are unbound.
Note:
1. Start the service using startservice ()
Intent intent = new intent (demoactivity. This, demoservice. Class );
Startservice (intent );
2. Context. bindservice () Startup
Intent intent = new intent (demoactivity. This, demoservice. Class );
Bindservice (intent, Conn, context. bind_auto_create );
// Unbindservice (conn); // unbind