Li Ning China Water Conservancy and hydropower press Font: T | T
Chapter 4 Android services in the complete handout on Android/ophone development. This chapter mainly introduces the Service Technology in the Android system. Service is one of the four application components in Android. Many system services are provided in the Android system. Through these system services, more complex functions can be implemented, such as monitoring incoming calls and gravity sensing. This section describes the service lifecycle.
AD:
8.1.1 service Lifecycle
The project directory of the sample code in this section is SRC \ ch08 \ ch08_servicelifecycle.
Like activity, Service also has a process from startup to destruction, but the process of service is much simpler than activity. The process from service startup to destruction only goes through the following three stages:
Create a service
Start Service
Destruction Service
A service actually inherits android. app. service class. After the service goes through the above three stages, it will call the three event methods in the service class for interaction. The three event methods are as follows:
- Public void oncreate (); // create a service
- Public void onstart (intent, int startid); // start the service
- Public void ondestroy (); // destroy the service
A service is created and destroyed once, but can be started multiple times. Therefore, the oncreate and ondestroy methods are called only once, while the onstart method is called multiple times.
The following is a service class. Let's take a look at the process from the beginning to the destruction of the Service lifecycle.
- Package net. blogjava. Mobile. Service;
-
- Import Android. App. Service;
- Import Android. content. intent;
- Import Android. OS. ibinder;
- Import Android. util. log;
-
- // Myservice is a service class, which must be inherited from the Android. App. Service class.
- Public class myservice extends Service
- {
- @ Override
- Public ibinder onbind (intent)
- {
- Return NULL;
- }
- // Call this method when the service is created for 1st times
- @ Override
- Public void oncreate ()
- {
- Log. D ("myservice", "oncreate ");
- Super. oncreate ();
- }
- // Call this method when the service is destroyed
- @ Override
- Public void ondestroy ()
- {
- Log. D ("myservice", "ondestroy ");
- Super. ondestroy ();
- }
- // Call this method when the service starts
- @ Override
- Public void onstart (intent, int startid)
- {
- Log. D ("myservice", "onstart ");
- Super. onstart (intent, startid );
- }
- }
In myservice, three life cycle methods in the service class are overwritten, and corresponding log information is output in these methods, so that you can more easily observe the call of the event method.
When writing Android Application Components, you should note that no matter what components (such as activity and Service) you write, you must configure them in the androidmanifest. xml file. The myservice class is not used as an example. To configure this service class, you only need to add the following code in the <Application> tag of the androidmanifest. xml file:
- <Service android: enabled = "true" Android: Name = ". myservice"/>
The value of the Android: enabled attribute is true, indicating that the myservice service is active. Although myservice is currently activated, the system still does not start myservice. To start this service. The startservice method must be explicitly called. To stop the service, you must call the stopservice method explicitly. The Code is as follows:
- Public void onclick (view)
- {
- Switch (view. GETID ())
- {
- Case R. Id. btnstartservice:
- Startservice (serviceintent );
// Click Start service to start the service.
- Break;
- Case R. Id. btnstopservice:
- Stopservice (serviceintent );
// Click STOP service to stop the service.
- Break;
- }
- }
Serviceintent is an intent object used to specify the myservice service. The code for creating this object is as follows:
- Serviceintent = new intent (this, myservice. Class );
After running the example in this section, the page shown in 8.1 is displayed.
|
Figure 8.1 start and stop a service |
After you click Start Service 1st, the message column of the logcat view in the ddms perspective will output the following two lines:
- Oncreate
- Onstart
Click Stop Service to output the following information in the message column:
- Ondestroy
Next, retest this example by clicking the button in the order below.
[Start service] → [Stop Service] → [start service] → [start service] → [start service] → [Stop Service]
After testing the program, the output information shown in 8.2 is displayed. You can see that the oncreate method is called only after you click the Start Service button for 1st times. If you click the Start Service button multiple times without clicking the Stop Service button, the system only calls the oncreate and onstart methods when you click the Start Service button 1st times. When you click this button again, the system only calls the onstart method instead of the oncreate method again.
|
Figure 8.2 service lifecycle Method Invocation |
After discussing the service lifecycle, summarize the steps for creating and starting the service. To create and start a service, follow these three steps:
(1) Write a service class that must be inherited from Android. App. Service. The service class involves three life cycle methods, but these three methods are not necessarily covered in the subclass. You can decide which life cycle methods to use based on different requirements. There is an onbind method in the service class. This method is an abstract method and must be overwritten in the service subclass. This method is called when the activity is bound to the Service (detailed in section 8.1.3 ).
(2) In androidmanifest. use the <service> label in the XML file to configure the service. Generally, set the Android: enabled attribute value of the <service> label to true, and use Android: the name attribute specifies the service class name created in step 1.
(3) If you want to start a service, use the startservice method. To stop a service, use the stopservice method.