Android Service life cycle and usage notes

Source: Internet
Author: User

A. Basic Knowledge

Services are generally divided into two types:

1: Local Service for internal applications. The service can call Context.startservice () to start and call Context.stopservice () to end. Internally you can call Service.stopself () or Service.stopselfresult () to stop yourself. No matter how many times startservice () is called, only one StopService () is called to stop.

2: Remote service is used between applications within the Android system. Interfaces can be defined and exposed for other applications to operate. The client establishes a connection to the service object and invokes the service through that connection. Call the Context.bindservice () method to establish a connection and start to call Context.unbindservice () to close the connection. Multiple clients can bind to the same service. If the service is not loaded at this time, bindservice () loads it first.
Available for reuse by other applications, such as defining a weather forecast service, and providing calls to other applications.

So let's look at the service life cycle first:

Context.startservice ()->oncreate ()->onstartcommand ()->service running--call Context.stopservice () OnDestroy () context.bindservice ( )

From the appeal can be known respectively corresponding to local, as well as remote, also corresponding to different ways to start the service.

two. precautions

2.1 The same service, multiple launches, the actual process of service execution

The first time you start the service, run OnCreate-->onstartcommand

After the service is started, the service only executes Onstartcommand

During the actual use, the data is passed through intent and executed in Onstartcommand.

Android Service life cycle and usage notes

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.