Binding Service Learning

Source: Internet
Author: User

The binding service is primarily the other component binding service (such as activity), then sends the request and receives the return. This service is mainly used as a servant of other components, no longer backstage unlimited

To run. The key to learning is how to bind and communicate between services and components.

How binding to a service is an implementation of a binding service class that allows other components to bind and communicate with him. To provide a binding for a service, you must implement the Onbind callback method. This method returns IBinder, which defines the program interface that the client can communicate with the service.
The client can call Bindservice to bind to the service, and to do so must provide the serviceconnection implementation, serviceconnection the listener and the connection to the service. Bindservice immediately returns without a value. However, when the system creates the client and the active connection, the system calls Serviceconnection's Onserviceconnected method, which transmits ibinder to communicate with the service.
Multiple clients can bind to the service at one time, whereas the Onbind method that invokes the service when the first client binds is receiving ibinder. It then transmits the same ibinder to other clients without calling Onbind. (Obviously this is the system to help us deal with, that is, the system to help us deal with a lot).
The most important thing to create a binding service binding service is to define the interface in the Onbind callback method. There are three ways to define this interface: (1) If the service is private to the program, and the client is running in the same process. Inherits the binder class and returns an instance of him in Onbind. The client receives this binder and can directly access the available public methods of the binder implementation or service. If the service is just a back-end worker in our program, this is the preferred option. Only our services are used by other applications or across processes to not use this approach. This method should be the most common.
(2) Use a messenger. If you want to cross-process, you need to use Messenger. (3) Aidl, not commonly used.
Inheritance binder most commonly used, we learn under: three parts:
    1. In your service, create a instance of that Binder either:
      • Contains public methods, the client can call
      • Returns the current Service instance, which have public methods theclient can call
      • Or, returns an instance of another class hosted by the service with public methods theclient can call
    2. Return this instance of the Binder onBind() callback method.
    3. In the client, receive the from the Binder onServiceConnected() callback method Andmake calls to the bound service using the methods Provid Ed.

 Public class LocalService extends Service {    //Binder given to clients    Private Final IBinderMbinder= New Localbinder();    //Random number generator    Private Final RandomMgenerator= New Random();    /** * Class used for the client Binder.     Because We know this service always * runs in the same process as it clients, we don ' t need to deal with IPC. */     Public class Localbinder extends Binder {        LocalServiceGetService() {            //Return This instance of LocalService so clients can call public methods            return LocalService. This;        }    }    @Override     Public IBinderOnbind(IntentIntent) {        returnMbinder;    }    /** method for clients * /     Public intGetRandomNumber() {      returnMgenerator.Nextint( -);    }}

The difference between a bound service is the ability to gain access to the service's methods by getting the returned service instance from binder.




Binding Service Learning

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.