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 plaincopyprint?
  1. package com.cao.android.demos.binder.aidl;
  2. import com.cao.android.demos.binder.aidl.AIDLActivity;
  • interface AIDLService {
  • void registerTestCall(AIDLActivity cb);
  • void invokCallBack();
  • }

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 plaincopyprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150

  1. private final AIDLService.Stub mBinder = new AIDLService.Stub() {
  2. @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;
  • }
  • };

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 plaincopyprint?
  1. AIDLService mService;
  2. 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;
  • }
  • };

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

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

文章仓促而成,有什么疑问欢迎大家一起讨论。

转载于:https://www.cnblogs.com/lz-cswb/archive/2012/04/29/2476349.html

android 中的aidl相关推荐

  1. android中的AIDL进程间通信

    关于IPC应该不用多介绍了,Android系统中的进程之间不能共享内存,那么如果两个不同的应用程序之间需要通讯怎么办呢?比如公司的一个项目要更新,产品的需求是依附于当前项目开发一个插件,但是呢这个插件 ...

  2. Android中Parcel的分析以及使用

    简单点来说:Parcel就是一个存放读取数据的容器, android系统中的binder进程间通信(IPC)就使用了Parcel类来进行客户端与服务端数据的交互,而且AIDL的数据也是通过Parcel ...

  3. Android应用中通过AIDL机制实现进程间的通讯实例

    Android中,每个应用程序都有自己的进程,当需要在不同的进程之间传递对象时,该如何实现呢?显然,Java中是不支持跨进程内存共享的,因此要传递对象,需要把对象解析成操作系统能够理解的数据格式,以达 ...

  4. Android中AIDL的使用详解

    Git: https://github.com/whtchl/AidlTemplate AIDL用来做什么 AIDL是Android中IPC(Inter-Process Communication)方 ...

  5. Android中进程间通讯 AIDL

    Android中进程间通讯 AIDL IDL Interface Description Language  接口描述语言 AIDL Android IDL 适用场景:    client进程必须是A ...

  6. android studio 跨进程,Android IPC机制(三)在Android Studio中使用AIDL实现跨进程方法调用...

    本文首发于微信公众号「后厂技术官」 在上一篇文章Android IPC机制(二)用Messenger进行进程间通信中我们介绍了使用Messenger来进行进程间通信的方法,但是我们能发现Messeng ...

  7. android lcd 显示图片,Android开发中通过AIDL文件中的方法打开钱箱,显示LCD屏幕

    下载相关 资源文件 ,在项目中新建如下层级的文件夹,将源文件中的AIDL文件放入其中. ICallback:打印服务执行结果的回调 ITax:打印服务执行结果的回调 ILcdCallback:顾显反馈 ...

  8. Android中Service深入学习

    概述 1.当用户在与当前应用程序不同的应用程序时,Service可以继续在后台运行. 2.Service可以让其他组件绑定,以便和它交互并进行进程间通信. 3.Service默认运行在创建它的应用程序 ...

  9. Android:学习AIDL,这一篇文章就够了(下)

    前言 上一篇博文介绍了关于AIDL是什么,为什么我们需要AIDL,AIDL的语法以及如何使用AIDL等方面的知识,这一篇博文将顺着上一篇的思路往下走,接着介绍关于AIDL的一些更加深入的知识.强烈建议 ...

最新文章

  1. 自我监督学习:AI技术的未来发展方向
  2. java识别文件类型_在Java中识别文件类型
  3. ABAP从数据库中删除行
  4. python语言打印菱形_Python打印菱形
  5. 看看这套WPF开源基础控件库:WPFDevelopers
  6. 字段缺失_区分Protobuf 3中缺失值和默认值
  7. Sharepoin学习笔记—架构系列--01 Sharepoint的网页(Page),网页解析(Parsing)与解析安全处理(Security)
  8. 高考考入北大与普通大学考研进北大,有区别吗?
  9. 天文学专业在什么时候学计算机,南京大学在985排名第几?南京大学最牛的专业是天文系吗?...
  10. 【Java】设计模式在Gourmet咖啡系统中的应用
  11. mysql 排序取前4,mysql分组取每组前几条记录(排序)
  12. win10英文系统一键装机教程
  13. Spring的 @ExceptionHandler注解无效问题
  14. openproj不能修改日期的原因分析与解决
  15. 营销工具-优惠券相关设计思路
  16. 天轰穿Visual Studio2005入门.Net2.0系列视频教程
  17. HTML下拉菜单代码实现
  18. windows Delete键和Backspace键的区别
  19. 联想服务器安装win10系统安装教程,联想笔记本安装win10系统图文教程
  20. kasp技术原理_SNP检测Massarray法怎么样?中高通量大样本适用吗?

热门文章

  1. MongoDB空间分配
  2. 深度解析iPhone Category用法
  3. Java 百鸡百钱题
  4. contos 安装vim自动补全插件 YCM YouCompleteMe
  5. Incomedia WebSite X5 17中文版
  6. 应用程序下载地址汇总
  7. 【codevs 1315】1315 摆花2012年NOIP全国联赛普及组(dp)
  8. Spark系列(八)Worker工作原理
  9. 分区时磁盘上没有足够的空间完成此操作的解决方法
  10. Aspose Cell设置Excel单元格背景色