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.

第四步告诉你怎么在client怎样调用服务端得aidl描写叙述的接口对象。doc仅仅告诉我们须要实现Service.onBind(Intent)方法,该方法会返回一个IBinder对象到client。绑定服务时不是须要一个ServiceConnection对象么。在没有了解aidl使用方法前一直不知道它是什么作用,事实上他就是用来在client绑定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对象,详细能够看我后面提供的演示样例代码,须要注意在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 AIDL使用特定的解释的更多相关文章

  1. Android aidl Binder框架浅析

      转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38461079 ,本文出自[张鸿洋的博客] 1.概述 Binder能干什么?B ...

  2. Android AIDL自动生成Java文件测试

    /******************************************************************************** * Android AIDL自动生成 ...

  3. Using self-defined Parcelable objects during an Android AIDL RPC / IPC call

    Using self-defined Parcelable objects during an Android AIDL RPC / IPC call In my previous post “Usi ...

  4. Android中的动画具体解释系列【4】——Activity之间切换动画

    前面介绍了Android中的逐帧动画和补间动画,并实现了简单的自己定义动画.这一篇我们来看看怎样将Android中的动画运用到实际开发中的一个场景--Activity之间跳转动画. 一.定义动画资源 ...

  5. Android Google Map v2具体解释:开发环境配置

    Android Google Map v2具体解释:开发环境配置                                       --转载请注明出处:coder-pig 说在前面: 说到地 ...

  6. log4net使用特定的解释

    说明:该程序演示如何使用log4net记录日志信息. log4net它是-known开源组件的日志记录功能.使用log4net可以很容易地将信息记录到文件.控制台.Windows事件日志和数据库(含有 ...

  7. Android系统APN配置具体解释

     Android 系统APN配置具体解释 这些天一直在调系统原生的Settings.apk里面APN配置的问题.在设置里面手动添加了APN配置选项.可是在界面上还是看不到.所以跟了下代码.原以为就是简 ...

  8. ANDROID L——Material Design详细解释(UI控制)

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! Android L: Google已经确认Android L就是Android Lolli ...

  9. Android开发之异步具体解释(二)之AsyncTask

    请尊重他人的劳动成果,转载请注明出处:Android开发之异步具体解释(二)之AsyncTask http://blog.csdn.net/fengyuzhengfan/article/details ...

随机推荐

  1. erlang shell表格数据对齐

    近期在erlang shell做一些測试,为了让測试结果数据显得更直观,想对齐须要打印的数据,做成像表格一样的效果. 開始的想法是在数据中插入tab. 当然,erlang也有对tab的支持,但实际效果 ...

  2. Steps UVA 846

    说说:此题要求求出从整数x到达整数y所要经过的最短步数,且第一步和最后一步必须为一,同一时候每一步都比前一步多一步,少一步或一样.如果想搞清楚每一步详细是如何走的,那么这道题是相当麻烦的.考虑到前后两 ...

  3. JavaScript 常见陷阱

    JavaScript中的一些特性和通常我们想象的不太一样.这里我总结了一些有悖直觉的语言特性. 1 数组 1.1 数组的遍历 在直接支持for a in b的语言中,比方Python/Ruby里的a的 ...

  4. C# Windows Phone App 开发,自制LockScreen 锁定画面类别(Class),从【网路图片】、【Assets资源】、【UI】修改锁定画面。

    原文:C# Windows Phone App 开发,自制LockScreen 锁定画面类别(Class),从[网路图片].[Assets资源].[UI]修改锁定画面. 一般我们在开发Windows ...

  5. ubuntu中KDE与GNOME安装切换

    转载:http://apps.hi.baidu.com/share/detail/18919303 1.在Ubuntu中安装KDE桌面命令 sudo apt-get install kUbuntu-d ...

  6. MapReduce(十五): 从HDFS阅读本文的源代码分析

    以Map任务读取文本数据为例: 1)   LineRecordReader负责对文件切割的定位,以及对读取每一行内容的封装供用户Map任务使用.每次在定位在文件里不为0的位置时,多读取一行,由于前一个 ...

  7. 冒泡排序----java实现

    冒泡排序思路:第1次:顺序比较从第0个到第len个(相邻两个)元素并把大的放后面,第一次进行完后,最大                                         的元素会在最后: ...

  8. Android复制iPhone日期和时间选择器

    看效果图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGluZ2xvbmd4aW4yNA==/font/5a6L5L2T/fontsize/400/fi ...

  9. Windows Phone 的控件倾斜效果

    原文:Windows Phone 的控件倾斜效果 Windows Phone 7的系统设置里,按钮都有一个点击倾斜的效果,但自己添加的控件就没有.但微软提供了这个效果的代码:TiltEffect MS ...

  10. STL源代码剖析(一) - 内存分配

    Allocaor allocator 指的是空间配置器,用于分配内存.STL中默认使用SGI STL alloc作为STL的内存分配器,尽管未能符合标准规格,但效率上更好.SGI STL也定义有一个符 ...