Android talk about aidl communication mechanism

Source: Internet
Author: User

Service side:

The first is to write a aidl file, note that Aidl only supports methods, cannot define static members, and that methods cannot have modifiers such as public, aidl run methods have any type of parameters and return values, and in Java types, the following types do not need to import packages when they are used , basic data type, String, Map, List. Of course, to avoid errors, it is recommended that you import packages whenever you use them.

Then start a service and register on the server and write an arbitrary class to implement the Java interface of aidl file generation stub!

Finally, instantiate your arbitrary class in the service and return the object of any class on the Onbind (Intent a) Method!

Client:

First copy your Aidl file on the server, and guarantee the package name.

Then call the Bindservice method to bind the method you created on the server Mcontext.bindservice (intent, mserviceconnection, 0);

Among them Mserviceconnection is the focus:

Private Serviceconnection mserviceconnection = new Serviceconnection () {

@Override
public void onserviceconnected (componentname name, IBinder service) {//is called when the server is connected
Mservice = IRemoteService.Stub.asInterface (service);

}

@Override
public void onservicedisconnected (componentname name) {//not connected on call
Mservice = null;
}
};

Where Mservice is the server's Aidl object, which we can assign to the service-side method

One of the intent to be aware of the need to provide a intent-filter to match the request is legitimate, so we in the client access to the service, we must also pass the matching action to include the intent Oh.

Android talk about aidl communication mechanism

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.