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
  1. packagecom.cao.android.demos.binder.aidl;
  2. importcom.cao.android.demos.binder.aidl.AIDLActivity;
  3. interfaceAIDLService{
  4. voidregisterTestCall(AIDLActivitycb);
  5. voidinvokCallBack();
  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.
编译你的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] view plain copy
  1. privatefinalAIDLService.StubmBinder=newAIDLService.Stub(){
  2. @Override
  3. publicvoidinvokCallBack()throwsRemoteException{
  4. Log("AIDLService.invokCallBack");
  5. Rect1rect=newRect1();
  6. rect.bottom=-1;
  7. rect.left=-1;
  8. rect.right=1;
  9. rect.top=1;
  10. callback.performAction(rect);
  11. }
  12. @Override
  13. publicvoidregisterTestCall(AIDLActivitycb)throwsRemoteException{
  14. Log("AIDLService.registerTestCall");
  15. callback=cb;
  16. }
  17. };

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.
第四步告诉你怎么在client怎样调用服务端得aidl描写叙述的接口对象。doc仅仅告诉我们须要实现Service.onBind(Intent)方法,该方法会返回一个IBinder对象到client,绑定服务时不是须要一个ServiceConnection对象么。在没有了解aidl使用方法前一直不知道它是什么作用,事实上他就是用来在client绑定service时接收service返回的IBinder对象的:

[java] view plain copy
  1. AIDLServicemService;
  2. privateServiceConnectionmConnection=newServiceConnection(){
  3. publicvoidonServiceConnected(ComponentNameclassName,IBinderservice){
  4. Log("connectservice");
  5. mService=AIDLService.Stub.asInterface(service);
  6. try{
  7. mService.registerTestCall(mCallback);
  8. }catch(RemoteExceptione){
  9. }
  10. }
  11. publicvoidonServiceDisconnected(ComponentNameclassName){
  12. Log("disconnectservice");
  13. mService=null;
  14. }
  15. };

mService就是AIDLService对象,详细能够看我后面提供的演示样例代码。须要注意在client须要存一个服务端实现了的aidl接口描写叙述文件。可是client仅仅是使用该aidl接口,不须要实现它的Stub类,获取服务端得aidl对象后mService = AIDLService.Stub.asInterface(service);,就能够在client使用它了。对mService对象方法的调用不是在client运行。而是在服务端运行。

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临时没有做深入研究。


作出说明

样例实现了一个AIDLTestActivity。AIDLTestActivity通过bindservice绑定一个服务AIDLTestService,通过并获取AIDLTestActivity的一个aidl对象AIDLService。该对象提供两个方法,一个是registerTestCall注冊一个aidl对象。通过该方法,AIDLTestActivity把本身实现的一个aidl对象AIDLActivity传到AIDLTestService。在AIDLTestService通过操作AIDLActivity这个aidl远端对象代理,使AIDLTestActivity弹出一个toast。完整样例见我上传的资源:

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

文章匆匆做,有欢迎大家讨论任何问题。

版权声明:本文博客原创文章,博客,未经同意,不得转载。

更多相关文章

  1. Android源码(9) --- Binder(3) AIDL使用
  2. Android四大组件之ContentProvider(一)
  3. Android(安卓)使用根据手机屏幕来进行比例拉伸接口获取的照片
  4. Android应该掌握的高级技巧
  5. android存储本地数据大全
  6. Android(安卓)获取调用接口的包名
  7. 第十三篇 Android(安卓)系统电话管理机制与架构二
  8. android中fragment与activity之间通信
  9. Android(安卓)Camera 架构简析1

随机推荐

  1. Android外派(安卓外派) — 长年提供安卓开
  2. android 用到的技巧集
  3. Android lint 删除无用图片文件和配置文
  4. 【Android】Java四种线程池的使用
  5. android 笔记 --- 自定义Android主题风格
  6. android 瀛︿範绗旇10鈥斺€擷ML瑙f瀽
  7. android 网络图片双缓存
  8. Android签到功能的实现
  9. [Android实例] Android 开发者面试题-广
  10. Android L Developer Preview