Android talks about the Aidl communication mechanism and androidaidl

Source: Internet
Author: User

Android talks about the Aidl communication mechanism and androidaidl

Server:

First, write an aidl file. Note that AIDL only supports methods, does not support defining static members, and does not support public modifiers; the AIDL running method has any type of parameters and return values. In java, the following types do not need to be imported (import). The basic data types include String, Map, and List. to avoid errors, we recommend that you import the package as long as you use it.

Start a service on the server and register it. Compile a JAVA interface Stub that can be generated by any class to implement AIDL file!

Finally, instantiate any of your classes in the service and return the objects of any of your classes on the onBind (Intent a) method!

Client:

First, copy the complete aidl file on the server end and ensure that the package name is the same.

Then call the bindservice method to bind the method mContext. bindService (intent, mServiceConnection, 0) You created on the server );

Among them, mServiceConnection is the focus:

Private ServiceConnection mServiceConnection = new ServiceConnection (){

@ Override
Public void onServiceConnected (ComponentName name, IBinder service) {// called when connecting to the server
MService = IRemoteService. Stub. asInterface (service );

}

@ Override
Public void onServiceDisconnected (ComponentName name) {// No connected call
MService = null;
}
};

MService is the aidl object of the server. You can assign values to the methods of the server.

The uploaded Intent must provide an intent-filter to match whether the request is valid. Therefore, when the client accesses the service, it must also pass the Intent that matches the action.

 

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.