Android learning Note 3: differences between context. startservice () and context. bindservice () in Service (1)

Source: Internet
Author: User

The service cannot run on its own. You need to call the context. startservice () or context. bindservice () method to start the service.

Both methods can start the service, but they are used in different places:

(1) The startservice () method is used to start the service. There is no association between the caller and the service. Even if the caller exits, the service can still run.

(2) Use the bindservice () method to start the service. The caller and the service are bound together. Once the caller exits, the service is terminated.

(3) If you use the startservice () method to start the service, the system will first call the oncreate () method of the service before it is created, and then call the onstart () method. If the service has been created before the onstart () method is called, multiple call of the startservice () method will not lead to multiple creation of the service, but will lead to multiple calls of the onstart () method. A service started using the startservice () method can only end the service by calling the context. stopservice () method. The ondestroy () method is called when the service ends.

(4) If you use the bindstart () method to start the service, the system will first call the oncreate () method of the service before it is created, and then call the onbind () method, the caller and the service are bound together. If the caller exits, the system first calls the onunbind () method of the service and then calls the ondestroy () method. If the service has been bound before the bindservice () method is called, multiple calls to the bindservice () method will not result in multiple service creation and binding (that is, oncreate () and onbind () method is not called multiple times ). If the caller wants to unbind from the service being bound, call the unbindservice () method. Calling this method also causes the system to call the onunbind () and ondestroy () Methods of 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.