The difference between the Android startup Service (StartService and Bindservice) _android

Source: Internet
Author: User

The Android service lifecycle can lead to innovation in mobile devices, allowing users to experience the most superior mobile services, only activated when broadcast receivers executes this method, which is inactive when onreceive () returns.

If no program stops it or it stops itself, the service will run all the time. In this mode, the service starts at the call Context.startservice () and stops at Context.stopservice (). Service can stop itself by calling the Android Service Lifecycle () or Service.stopselfresult (). No matter how many times you call StartService (), you can stop the service by simply calling StopService () once.

Can be invoked by an external program via an interface. The external program establishes a connection to the service and operates the service through a connection. The setting up of the connection is started at Context.bindservice () and ends at Context.unbindservice (). Multiple clients can bind to the same service, and if the service does not start, Bindservice () can choose to start it.

The service cannot run itself, and you need 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 contexts. Using the StartService () method to enable the service, there is no connection between the caller and the service, and 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 simultaneously" characteristics.

If you intend to start the service using the Context.startservice () method, the service's OnCreate () method is invoked, followed by the OnStart () method, when the service is not created. If the service has been created before the StartService () method is invoked, calling the StartService () method multiple times does not cause the service to be created multiple times, but will cause 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 invoked at the end of the service.

If you intend to start the service using the Context.bindservice () method, the service's OnCreate () method is invoked, followed by the Onbind () method, when the service is not created. This time the caller and the service are bound together and the caller exits, the system invokes the Onunbind () method of the service, and then calls the OnDestroy () method. If the service is already bound before the Bindservice () method is invoked, calling the Bindservice () method multiple times does not cause the service and the bindings to be created multiple times (that is, the OnCreate () and Onbind () methods are not invoked more than once). If the caller wants to unbind the service being bound, the Unbindservice () method can be invoked, and invoking the method also causes the system to invoke the Onunbind ()-->ondestroy () method of the service.

Summary: Two ways to start Service and the difference

Life cycle methods of service OnCreate, OnStart, OnDestroy

There are two ways to start a service, and their impact on the service lifecycle is different.

1 through StartService

Service will experience onCreate-> OnStart

When StopService, direct OnDestroy.

The service runs in the background if it is the caller who exits without invoking StopService. The next caller can stopservice up again.

2 through Bindservice

Service will only run OnCreate, at which point the caller and service are bound together

The caller exits, and Srevice calls onunbind->ondestroyed the so-called binds together and dies. And this way it can also make
Callers (for example) invoke other methods on the service.

Related Article

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.