Chapter 4 service Basics

Source: Internet
Author: User

The Service does not have the actual interface, but has been running in the android background. The main purpose of the service is to run in the background and access across processes. A service is like an activity that has its own life cycle. Choose create> Start> destroy.

A service actually inherits Android. App. Service. A service is created and destroyed once, but can be started multiple times.

Service also needs to be configured in androidmanifest, add the following in the <Application> label:Code:

<Service android: Name = ". myservice (class inherited from the service)" Android: Enable = "true"> the property of Android: Enable is true, indicating that the myservice service is active. Even in the excited state, we should call the startservice method explicitly; Use the stopservice method to stop the service.

To create and start a service, follow these three steps:

A. Write a service class that must inherit from Android. App. Service. Service involves three lifecycle methods, but these three methods can be called based on actual conditions. There is also an ibind method in service, which is an abstract method and must be overwritten by subclass. This method is called when the activity is bound to the service.

B. in androidmanifest. the <service> tag is used in the XML file to configure the service. Generally, you need to set the Android: Enable attribute of the <service> tag to true and use Android: the name attribute specifies the class name created in step 1.

C. enable the service by using the startservice () method, and disable the service by using the stopservice () method.

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.