Full resolution of service in ANDROID development

Source: Internet
Author: User

    • definition: service, is one of the four components of Android, belongs to the computational type component
    • role: provide services that require long-term operation in the background
    • Life cycle:
        

in the The service life cycle, commonly used are:

(1) 4 Methods for manual invocation

Calling methods Manually

Role

StartService ()

Start the service

StopService ()

Close Service

Bindservice ()

Binding Service

Unbindservice ()

Unbind Service

(2) 5 methods of automatic call

Methods for internal automatic calls

Role

Oncreat ()

Create a service

Onstartcommand ()

Start Service

OnDestroy ()

Destruction Services

Onbind ()

Binding Service

Onunbind ()

Unbind Service

    • NOTE: You must declare it in the Androidmanifest.xml file. (All four Android components need to be declared in this configuration file)
    • Usage scenarios:

(1) Only start a background service, a long-term task, the use of startservice;

(2) only want to communicate with backstage service, use Bindservice;

(3) If a service is started and bound, the service will always run in the background, using both StartService and Bindservice.

First, OnCreate is always called only once, regardless of how it is called. How many times will the service's Onstartcommand method be called, corresponding to the number of StartService calls. The termination of the service requires both Unbindservice and stopservice to be called simultaneously.

whether StartService and the order in which the bindservice is called, if Unbindservice is called first, the service does not terminate automatically, the service terminates after the StopService is called, and if StopService is called first, the service does not terminate at this time.

and then call Unbindservice or the previous call to Bindservice The service stops automatically after the context does not exist (such as when the activity is finished).

    • Contrast

(1) The difference between Service and thread

Note: The service and thread are typically used in conjunction, creating a sub-thread (worker thread) in the service to handle time-consuming operational logic

(2) The difference between service and Intentservice

    • Instance

Full resolution of service in ANDROID development

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.