Android: Life cycle of the service

Source: Internet
Author: User

9.4 Life cycle of service

In the previous chapters we learned about activities and the life cycle of fragments. Similarly, services have their own lifecycles, and methods such as OnCreate (), Onstartcommand (), Onbind (), and OnDestroy (), which we used earlier, are methods that can be recalled during the life cycle of a service.

Once the Context's StartService () method is called at any point in the project, the corresponding service will start up and callback the Onstartcommand () method. If the service has not been created before, the OnCreate () method will be executed prior to the Onstartcommand () method. After the service is started, it remains running until the StopService () or Stopself () method is called. Note Although the StartService () method is called every time, Onstartcommand () executes once, but there is actually only one instance per service. So no matter how many times you call the StartService () method, simply call the StopService () or Stopself () method and the service will stop.

Alternatively, you can call the Context's Bindservice () to get a persistent connection to a service, and then callback the Onbind () method in the service. Similarly, if the service has not been created before, the OnCreate () method is executed before the Onbind () method. The caller can then obtain an instance of the IBinder object returned in the Onbind () method so that it can communicate freely with the service. As long as the connection between the caller and the service is not broken, the service remains in a running state.

When the StartService () method is called and the StopService () method is called, the OnDestroy () method in the service executes, indicating that the service has been destroyed. Similarly, when the Bindservice () method is called and the Unbindservice () method is called, the OnDestroy () method executes, both of which are well understood. However, it is important to note that it is entirely possible for a service to call both the StartService () method and the Bindservice () method, so how can the service be destroyed? Depending on the mechanism of the Android system, a service can be destroyed only after it has been started or bound, and it has been in a state of operation and must not satisfy both of these conditions. Therefore, the OnDestroy () method executes when the StopService () and Unbindservice () methods are called simultaneously in this case.

So you've gone through the life cycle of the service completely.

Android: Life cycle of the service

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.