8.1.1 service Lifecycle

Source: Internet
Author: User
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.


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:

  1. Public void oncreate (); // create a service
  2. Public void onstart (intent, int startid); // start the service
  3. 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.

  1. Package net. blogjava. Mobile. Service;
  3. Import Android. App. Service;
  4. Import Android. content. intent;
  5. Import Android. OS. ibinder;
  6. Import Android. util. log;
  8. // Myservice is a service class, which must be inherited from the Android. App. Service class.
  9. Public class myservice extends Service
  10. {
  11. @ Override
  12. Public ibinder onbind (intent)
  13. {
  14. Return NULL;
  15. }
  16. // Call this method when the service is created for 1st times
  17. @ Override
  18. Public void oncreate ()
  19. {
  20. Log. D ("myservice", "oncreate ");
  21. Super. oncreate ();
  22. }
  23. // Call this method when the service is destroyed
  24. @ Override
  25. Public void ondestroy ()
  26. {
  27. Log. D ("myservice", "ondestroy ");
  28. Super. ondestroy ();
  29. }
  30. // Call this method when the service starts
  31. @ Override
  32. Public void onstart (intent, int startid)
  33. {
  34. Log. D ("myservice", "onstart ");
  35. Super. onstart (intent, startid );
  36. }
  37. }

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:

  1. <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:

  1. Public void onclick (view)
  2. {
  3. Switch (view. GETID ())
  4. {
  5. Case R. Id. btnstartservice:
  6. Startservice (serviceintent );
    // Click Start service to start the service.
  7. Break;
  8. Case R. Id. btnstopservice:
  9. Stopservice (serviceintent );
    // Click STOP service to stop the service.
  10. Break;
  11. }
  12. }

Serviceintent is an intent object used to specify the myservice service. The code for creating this object is as follows:

  1. 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:

  1. Oncreate
  2. Onstart

Click Stop Service to output the following information in the message column:

  1. 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.


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.