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类文件中定义的接口

  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.
编译你的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类:

  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翻译成中文是存根的意思,注意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对象的:

  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就是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,完整例子见我上传的资源:

最新文章

  1. 延迟加载外部js文件,延迟加载图片(jquery.lazyload.js和echo,js)
  2. pip 安装插件慢的解决方法
  3. 滚动RollUp、压缩
  4. Spark系列—02 Spark程序牛刀小试
  5. java.util.ResourceBundle
  6. 自己封装的poi操作Excel工具类
  7. Eclipse下绿色安装插件Aptana、Swing
  8. C#实现下载功能,可用于软件自动更新
  9. MyEclipse 在loading workbench 启动卡死
  10. 黄聪:C#使用GeckoFx拦截监控Http数据
  11. ZOJ 4067 - Books - [贪心][2018 ACM-ICPC Asia Qingdao Regional Problem J]
  12. Spring Boot+Quartz实现一个实时管理的定时任务
  13. [Python] 当猎头遇上 Guido van Rossum
  14. twiested 及其他轮子
  15. shell script 脚本编程
  16. Dubbox服务demo
  17. 以太坊虚拟机(EVM)
  18. Operating System-Thread(3)用户空间和内核空间实现线程
  19. 第六章 memcached剖析
  20. 一些noip模拟题一句话题解

热门文章

  1. 列举一些常见的系统系能瓶颈 Common Bottlenecks
  2. JavaScript 对象与数组参考大全
  3. FFmpeg 如何探测网络流格式/如何从内存中获取数据
  4. Redis配置和常用命令
  5. hibernate中继承映射保存
  6. CentOS Linux防火墙配置及关闭
  7. windows 添加打印机
  8. Launcher代码大全
  9. 16位结构的CPU,8086给出物理地址的方法
  10. 跟着百度学PHP[8]-setcookie的其他参数学习