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

[java] view plaincopy

  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类:

[java] view plaincopy

  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对象的:

[java] view plaincopy

  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

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

时间: 2024-08-24 15:06:29

Android AIDL使用特定的解释的相关文章

Android aidl Binder框架浅析

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38461079 ,本文出自[张鸿洋的博客] 1.概述 Binder能干什么?Binder可以提供系统中任何程序都可以访问的全局服务.这个功能当然是任何系统都应该提供的,下面我们简单看一下Android的Binder的框架 Android Binder框架分为服务器接口.Binder驱动.以及客户端接口:简单想一下,需要提供一个全局服务,那么全局服务那端即是服务器接口,任何程序即客

打造支持apk下载和html5缓存的 IIS(配合一个超简单的android APP使用)具体解释

为什么要做这个看起来不靠谱的东西呢? 由于刚学android开发,还不能非常好的熟练控制android界面的编辑和操作,所以我的一个急着要的运用就改为html5版本号了,反正这个运用也是须要从server获取大量数据来展示在手机上面的,也就是说:必须联网,才干正常工作,于是想了一下,反正都要联网获取数据,为什么不直接用我相对熟悉一点的 html来做这个运用呢?省的花费不够用的时间去学习android界面的控制,于是就简单了:用蹩脚的手段做了一个android程序的启动欢迎界面,内页就是一个全屏的

Android AIDL开发

Introduction   在Android中, 每个应用程序都运行在自己的进程中,拥有独立的内存空间.但是有些时候我们的应用程序需要跟其它的应用程序进行通信,这个时候该怎么办呢?显然, Java中不允许跨进程内存共享.无法直接交换数据.Android中可以采用AIDL的方式实现进程间通信(interprocess communication(IPC)). Android Developer原文介绍如下:AIDL (Android Interface Definition Language)

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

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 “Using the Android Interface Definition Language (AIDL) to make a Remote Procedure Call (RPC) in Android” I’ve explained the basics on how inter-process c

Android AIDL 使用示例

介绍: AIDL 即  Android Interface Definition Language 使用: 1.新建.aidl文件 1 //AIDL 文件所在的包 2 package com.houny.demo_aidl.aidl; 3 4 //接口名必须和AIDL文件名一致 5 interface ISay{ 6 boolean Say(); 7 boolean SayInt(int i); 8 boolean SayString(String str); 9 } 2.新建Service,并

Android源代码下载方法具体解释

作者:张星 相信非常多下载过内核的人都对这个非常熟悉 git clone git://android.git.kernel.org/kernel/common.git kernel 可是这是在曾经,如今假设这么运行的话,会显演示样例如以下内容 Initialized empty Git repository in /home/star/working/kernel/.git/ android.git.kernel.org[0: 149.20.4.77]: errno=Connection ref

android aidl摘要

/*在Android中, 每个应用程序都有自己的进程,当需要在不同的进程之间传递对象时,该如何实现呢? 显然, Java中是不支持跨进程内存共享的.因此要传递对象, 需要把对象解析成操作系统能够理解的数据格式, 以达到跨界对象访问的目的.在JavaEE中,采用RMI通过序列化传递对象.在Android中, 则采用AIDL(Android Interface Definition Language:接口描述语言)方式实现. AIDL是一种接口定义语言,用于约束两个进程间的通讯规则,供编译器生成代码

Android AIDL自动生成Java文件测试

/******************************************************************************** * Android AIDL自动生成Java文件测试 * 说明: * 知道有aidl这东西已经挺久了,但是一直没有花时间来系统了解一下其工作机制,现在 * 花点时间一点一点验证一下其功能. * * 2016-5-8 深圳 南山平山村 曾剑锋 ***********************************************