Introduction to Android Development Service

Source: Internet
Author: User

Service life cycle method is less than activity, only OnCreate, OnStart, OnDestroy

We have two ways to start a service, and they have a different effect on the service life cycle.

  1 by StartService

Service will experience onCreate--OnStart

Direct OnDestroy when StopService.

If the caller exits without calling StopService, the service will always run in the background.

The next caller can still get up and stopservice.

  2 by Bindservice

The service will only run OnCreate, when the caller and service are bound together

When the caller exits, Srevice calls onunbind-->ondestroyed

The so-called binding together is a common survival.

Note: The OnCreate method of the service will only be called once,

Is that you no matter how many times the StartService and Bindservice,service only be created once.

If BIND is first, then start will run the service's OnStart method directly,

If first start, then bind will run the Onbind method directly. If you bind it first, stop it.

Can only first unbindservice, then StopService, so is the first start or bind behavior is a difference.

Services in Android and services in Windows are similar, services generally do not have user interface, it runs in the system is not easy to be detected by the user, you can use it to develop such as monitoring programs.

The service cannot run itself and needs to start the service by calling the Context.startservice () or Context.bindservice () method.

Both of these methods can start the service, but they are used in different situations. Using the StartService () method to enable the service, there is no connection between the caller and the service.

The service is still running even if the caller exits. Using the Bindservice () method to enable the service, the caller and the service are bound together, and once the caller exits, the service terminates, and there is a "no need to live at the same time Must Die" feature.

If you intend to start the service with the Context.startservice () method, the system calls the service's OnCreate () method first when the service is not created.

Then call the OnStart () method. If the StartService () method is called before the service has been created, calling the StartService () method multiple times does not cause the service to be created more than once.

However, it causes multiple calls to the OnStart () method. Services started with the StartService () method can only call the Context.stopservice () method to end the service, and the OnDestroy () method is called at the end of the service.

If you intend to start the service with the Context.bindservice () method, the system calls the service's OnCreate () method first when the service is not created.

Then call the Onbind () method. This time the caller and the service are bound together, the caller exits, and the system calls the service's Onunbind () method first.

Then call the OnDestroy () method. If the service has been bound before the Bindservice () method is called,

Calling the Bindservice () method multiple times does not cause the service and bindings to be created multiple times (that is, the OnCreate () and Onbind () methods are not called more than once).

If the caller wants to unbind the service being bound, it can call the Unbindservice () method, and calling the method will also cause the system to invoke the service's Onunbind ()-->ondestroy () method.

Introduction to Android Development 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.