Android Aidl Use detailed

Source: Internet
Author: User

1. What is Aidl:aidl is an abbreviation for Android Interface definition language, and it is understood that it is a description language of the internal process communication interface of Android, through which we can define the communication interface between processes
Icp:interprocess communication: Internal process communication

2. Since aidl can define and implement process communication, how do we use it? The steps are described in detail in document/android-sdk/docs/guide/developing/tools/aidl.html:

--1.create your. Aidl file-this file defines an interface (YOURINTERFACE.AIDL) that defines the methods and fields avail Able to a client.
To create your aidl file, I give an example of the Aidl file, which is defined as follows: It is similar to Java code, but it is important to note that it can refer to interfaces defined in other AIDL files, but cannot refer to interfaces defined in your Java class file.

[Java]View Plaincopy
    1. Package com.cao.android.demos.binder.aidl;
    2. Import com.cao.android.demos.binder.aidl.AIDLActivity;
    3. Interface Aidlservice {
    4. void Registertestcall (aidlactivity CB);
    5. void Invokcallback ();
    6. }

--2.add the. aidl file to your makefile-(the ADT Plugin for Eclipse manages this for you). Android includes the compiler, called Aidl, in the tools/directory.
Compile your aidl file, as long as it is developed in Eclipse, your ADT plugin will compile the Aidl file like a resource file into Java code generated in the Gen folder, without having to compile it manually: Compile and generate Aidlservice.java code like my example


--3.implement your interface methods-the Aidl compiler creates an interface in the Java programming language from your A IDL interface. This interface have an inner abstract class named Stub inherits the interface (and implements a few additional methods necessary for the IPC call). You must create a class this extends yourinterface.stub and implements the methods you declared in your. aidl file.
Implement the internal abstract class that you define in the Aidl interface stub,public static abstract class Stub extends Android.os.Binder implements Com.cao.android.demos.binder.aidl.AIDLService
The stub class inherits the binder and inherits the interface we defined in the Aidl file, we need to implement the interface method, and here is the stub class I implemented in the example:

[Java]View Plaincopy
  1. Private final Aidlservice.stub mbinder = new Aidlservice.stub () {
  2. @Override
  3. public void Invokcallback () throws remoteexception {
  4. Log ("Aidlservice.invokcallback");
  5. Rect1 rect = new Rect1 ();
  6. rect.bottom=-1;
  7. rect.left=-1;
  8. rect.right=1;
  9. rect.top=1;
  10. Callback.performaction (rect);
  11. }
  12. @Override
  13. public void Registertestcall (aidlactivity cb) throws remoteexception {
  14. Log ("Aidlservice.registertestcall");
  15. callback = CB;
  16. }
  17. };

Stub translation into Chinese is the meaning of the stub, note that the stub object is in the callee process, that is, the server process, at this point, the server Aidl service end of the code is complete.

--4.expose your interface to Clients-if "re writing a service, you should extend service and override Service.onbind ( Intent) to return a instance of your class that implements your interface.
The fourth step tells you how the client invokes the interface object described by the service-side Aidl, Doc only tells us that we need to implement the Service.onbind (Intent) method, which returns a IBinder object to the client, Does it not require a Serviceconnection object to bind the service, and I don't know what it does until I know the aidl usage, in fact he is used to receive the IBinder object that the service returns when the client binds the service:

[Java]View Plaincopy
  1. Aidlservice Mservice;
  2. Private Serviceconnection mconnection = new Serviceconnection () {
  3. public void onserviceconnected (componentname className, IBinder service) {
  4. Log ("Connect service");
  5. Mservice = AIDLService.Stub.asInterface (service);
  6. try {
  7. Mservice.registertestcall (Mcallback);
  8. } catch (RemoteException e) {
  9. }
  10. }
  11. public void onservicedisconnected (ComponentName className) {
  12. Log ("Disconnect service");
  13. Mservice = null;
  14. }
  15. };

Mservice is the Aidlservice object, specifically, you can see the example code that I provide later, you need to note that the client needs to save a server-side implementation of the Aidl interface profile, but the client just use the Aidl interface, do not need to implement its stub class, After getting the Aidl object mservice = AIDLService.Stub.asInterface (service), you can use it on the client, and the Mservice object method is not executed on the client, but on the server.

Using Java classes in 4.aidl requires implementing the Parcelable interface and declaring the class under the same package as the definition class:

I've defined the Rect1 class above.
You can then use the class in the Aidl interface.
Package com.cao.android.demos.binder.aidl;
Import Com.cao.android.demos.binder.aidl.Rect1;
Interface Aidlactivity {
void Performaction (in Rect1 rect);
}
Note that In/out's instructions, I used here to indicate input parameters, out did not try, why use in/out temporarily did not do in-depth research.

5.aidl using the complete example, in order to clear the instructions aidl use, I wrote here An example, examples refer to the blog:

Http://blog.csdn.net/saintswordsman/archive/2010/01/04/5130947.aspx

Make a note

Example implements a aidltestactivity,aidltestactivity binding a service aidltestservice through Bindservice, Through and get a Aidl object Aidlservice for Aidltestactivity, the object provides two methods, one is Registertestcall registers a Aidl object, through which the method, Aidltestactivity the realization of a Aidl object aidlactivity to Aidltestservice, aidltestservice the aidlactivity through the operation aidl the remote object agent, Make aidltestactivity pop up a toast, for a complete example see my uploaded resources:

http://download.csdn.net/source/3284820

The article rushed into, what is the question welcome to discuss together.

Original address: http://blog.csdn.net/stonecao/article/details/6425019

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.