Android interview question: how to enable service and how to disable service?

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.