1.什么是aidl:aidl是 Android Interface definition language的缩写,一看就明白,它是一种android内部进程通讯接口的描述语言,经过它咱们能够定义进程间的通讯接口 icp:interprocess communication :内部进程通讯 2.既然aidl能够定义并实现进程通讯,那么咱们怎么使用它呢?文档/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 available to a client. 建立你的aidl文件,我在后面给出了一个例子,它的aidl文件定义以下:写法跟java代码相似,可是这里有一点值得注意的就是它能够引用其它aidl文件中定义的接口,可是不可以引用你的java类文件中定义的接口 [java] view plain copy package com.cao.android.demos.binder.aidl; import com.cao.android.demos.binder.aidl.AIDLActivity; interface AIDLService { void registerTestCall(AIDLActivity cb); void invokCallBack(); } --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. 编译你的aidl文件,这个只要是在eclipse中开发,你的adt插件会像资源文件同样把aidl文件编译成java代码生成在gen文件夹下,不用手动去编译:编译生成AIDLService.java如我例子中代码 --3.Implement your interface methods - The AIDL compiler creates an interface in the Java programming language from your AIDL interface. This interface has an inner abstract class named Stub that inherits the interface (and implements a few additional methods necessary for the IPC call). You must create a class that extends YourInterface.Stub and implements the methods you declared in your .aidl file. 实现你定义aidl接口中的内部抽象类Stub,public static abstract class Stub extends android.os.Binder implements com.cao.android.demos.binder.aidl.AIDLService Stub类继承了Binder,并继承咱们在aidl文件中定义的接口,咱们须要实现接口方法,下面是我在例子中实现的Stub类: [java:showcolumns] view plain copy ·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150 private final AIDLService.Stub mBinder = new AIDLService.Stub() { @Override public void invokCallBack() throws RemoteException { Log("AIDLService.invokCallBack"); Rect1 rect = new Rect1(); rect.bottom=-1; rect.left=-1; rect.right=1; rect.top=1; callback.performAction(rect); } @Override public void registerTestCall(AIDLActivity cb) throws RemoteException { Log("AIDLService.registerTestCall"); callback = cb; } }; Stub翻译成中文是存根的意思,注意Stub对象是在被调用端进程,也就是服务端进程,至此,服务端aidl服务端得编码完成了。 --4.Expose your interface to clients - If you're writing a service, you should extend Service and override Service.onBind(Intent) to return an instance of your class that implements your interface. 第四步告诉你怎么在客户端如何调用服务端得aidl描述的接口对象,doc只告诉咱们须要实现Service.onBind(Intent)方法,该方法会返回一个IBinder对象到客户端,绑定服务时不是须要一个ServiceConnection对象么,在没有了解aidl用法前一直不知道它是什么做用,其实他就是用来在客户端绑定service时接收service返回的IBinder对象的: [java] view plain copy AIDLService mService; private ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { Log("connect service"); mService = AIDLService.Stub.asInterface(service); try { mService.registerTestCall(mCallback); } catch (RemoteException e) { } } public void onServiceDisconnected(ComponentName className) { Log("disconnect service"); mService = null; } }; mService就是AIDLService对象,具体能够看我后面提供的示例代码,须要注意在客户端须要存一个服务端实现了的aidl接口描述文件,可是客户端只是使用该aidl接口,不须要实现它的Stub类,获取服务端得aidl对象后mService = AIDLService.Stub.asInterface(service);,就能够在客户端使用它了,对mService对象方法的调用不是在客户端执行,而是在服务端执行。 4.aidl中使用java类,须要实现Parcelable接口,而且在定义类相同包下面对类进行声明: 上面我定义了Rect1类 以后你就能够在aidl接口中对该类进行使用了 package com.cao.android.demos.binder.aidl; import com.cao.android.demos.binder.aidl.Rect1; interface AIDLActivity { void performAction(in Rect1 rect); } 注意in/out的说明,我这里使用了in表示输入参数,out没有试过,为何使用in/out暂时没有作深刻研究。 5.aidl使用完整示例,为了清除说明aidl使用,我这里写了一个例子,例子参考了博客: http://blog.csdn.net/saintswordsman/archive/2010/01/04/5130947.aspx 做出说明 例子实现了一个AIDLTestActivity,AIDLTestActivity经过bindservice绑定一个服务AIDLTestService,经过并获取AIDLTestActivity的一个aidl对象AIDLService,该对象提供两个方法,一个是registerTestCall注册一个aidl对象,经过该方法,AIDLTestActivity把自己实现的一个aidl对象AIDLActivity传到AIDLTestService,在AIDLTestService经过操做AIDLActivity这个aidl远端对象代理,使AIDLTestActivity弹出一个toast,完整例子见我上传的资源: