Android Service Introduction and startup mode

Source: Internet
Author: User

1, the role of Android service:

Service is typically used to handle time-consuming operations , or background execution does not provide a user interface , for example: downloading, playing music.

2. life cycle of Android service:

The service life cycle is not as complex as the activity lifecycle, it has only OnCreate (), OnStart (), OnDestroy ( ) Three methods, and when the service is first started , The OnCreate (), OnStart () methods are called successively , when the service is stopped , the OnDestroy () method is executed, and it is important to note that if the service is started , when we start the program again to invoke the service, the OnCreate () method is not executed, but the OnStart () method is executed directly .

3. Two ways to start Android service:

①,StartService (): The same app calls the StartService () method anywhere to start the service, and then the system automatically calls OnCreate (), OnStart (), so that the service started will run in the background until the StopService () or Selfstop () method is called. In addition, if a Service has been started and other code wants to call the StartService () method again, the OnCreate () will not be executed, but the OnStart () will be executed again.

②,Bindservice (): When started with the Bindservice () method, the service is bound to the client and the client stops and the service stops.

 The difference between the two : StartService () is not associated with whether the client exits, even if the client stops running the service also runs in the background , but Bindservice () is bound to the client, The client exits the service and stops automatically .

Android Service Introduction and startup mode

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.