Android Service 3 (Bind Service)

Source: Internet
Author: User

 

As mentioned earlier, there are two ways to start a Service: startService and bindService. The former has already said how to use it. Therefore, this post is mainly about bind service. Here we will discuss only those services that are bound, and those services that are both startService () and bindService () are not in this scope.

① The Bind Service is like a server in the C/S architecture. Other components (such as Activity) are bound to it (through bindService () and can send requests to it, it can accept the response returned from it, and even provides the inter-process communication (IPC) function.

② To bind a service to another component, its onBind () method must be implemented and an IBinder object must be returned, other components can then communicate with the service through the IBinder object.

③ Multiple clients can be bound to the same service, but the onBind () method of the service is called only when the first client is bound to it, when another client is bound to it again, it does not call the onBind () method, but directly returns the IBinder object generated when it is called for the first time. In other words, onBind () is called only once in its lifecycle.

④ Shows the lifecycle of the Bind Service:

 

 

⑤ The Bind Service will not run permanently in the background, but when all components bound to it call unbindService () for unbinding, the system will stop it to recycle resources.

 

⑥ When we want to implement a Bind Service, the most important thing is to implement its onBind () method to return an IBinder object.

To be updated

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.