Talking about the communication mechanism of Android Aidl _android

Source: Internet
Author: User
Tags stub

Service side:

The first is to write a aidl file, note that Aidl only supports methods, cannot define static members, and methods do not have modifiers such as public; the Aidl Run method has any type of parameter and return value, in the Java type, the following types do not need to import packages (imports) when used , basic data types, String, Map, List. Of course, in order to avoid errors, it is recommended that you import the package as long as you use it.

Then start a service on the server and register to write an arbitrary class that implements the Java interface for Aidl file generation stub!

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

Client:

First of all, you copy the Aidl file on the server, and make sure the package name is the same.

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

Where Mserviceconnection is the focus:

Private Serviceconnection mserviceconnection = new Serviceconnection () {
@Override public
Void onserviceconnected (componentname name, IBinder service) {//when connecting to the server, call
Mservice = IRemoteService.Stub.asInterface ( Service);
}
The @Override public
void onservicedisconnected (componentname name) {//is not connected to invoke
Mservice = null;
}
;

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

The intent must pay attention to the need to provide a intent-filter to match the request is legitimate, so we access the service in the client, we must also pass the matching action intent Oh.

The above content is small series to introduce the Android aidl communication mechanism, I hope to help you!

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.