Android AIDL使用特定的解释
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类文件里定义的接口
- 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类:
- 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.
第四步告诉你怎么在client怎样调用服务端得aidl描写叙述的接口对象。doc仅仅告诉我们须要实现Service.onBind(Intent)方法,该方法会返回一个IBinder对象到client。绑定服务时不是须要一个ServiceConnection对象么。在没有了解aidl使用方法前一直不知道它是什么作用,事实上他就是用来在client绑定service时接收service返回的IBinder对象的:
- 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对象,详细能够看我后面提供的演示样例代码,须要注意在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临时没有做深入研究。
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
版权声明:本文博客原创文章,博客,未经同意,不得转载。
相关文章
- Android 面试精华题目总结
- android脚本工具,安卓脚本编程软件[通俗易懂]
- android 的hook技术,Android Native Hook技术(一)
- android okio使用方法,Android 开源框架 Okio 原理剖析「建议收藏」
- android中适配器的作用,适配器模式 在Android中的简单理解「建议收藏」
- iphone4装android,iPhone4可安装Android实现双系统启动.pdf
- Android NDK 开发 | CMake 使用手册 - 初见篇
- Android resource linking failed_android:authorities
- android应用程序_chrome Android
- android activitymanager 系统api_Android view
- android attrs获取_关于Android attrs 自定义属性的说明
- android消息通知更新(小红点,数字提醒)之badgeview
- android 测试神器 BugTags
- 【Android 应用开发】Android UI 设计之 TextView EditText 组件属性方法最详细解析
- 【Android 应用开发】AndroidUI设计 之 图片浏览器
- 【错误记录】Android 应用安装后出现两个启动图标 ( 排查应用及依赖库中配置了 android.intent.category.LAUNCHER 的 Activity )
- 【错误记录】Android Studio 编译报错 ( Could not resolve com.android.tools.build:gradle:7.4.2. )
- Android Studio 导入项目 出现安装Error:Cause: failed to find target with hash string ‘android-23’ 等错误详解手机开发
- 64位谷歌浏览器安卓版终于正式发布 但仅支持Android 10+和8GB+内存
- Android系统与Linux之间的联系(android和linux)
- Connecting Android to Oracle: The Ultimate Guide for Seamless Integration.(android连接oracle)
- Android系统自带样式(android:theme)
- Android中对于图片的内存优化方法
- Android中监听系统网络连接打开或者关闭的实现代码
- Android中的android:layout_weight使用详解
- Android动态添加menu菜单的简单方法
- Android中自定义ContentProvider实例
- android对话框弹出位置和透明度的设置具体实现方法
- android调用国家气象局天气预报接口json数据格式解释
- Android中删除Preference详解