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.