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
plain
copy

  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
plain
copy

  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
plain
copy

  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临时没有做深入研究。

作出说明

样例实现了一个AIDLTestActivity。AIDLTestActivity通过bindservice绑定一个服务AIDLTestService,通过并获取AIDLTestActivity的一个aidl对象AIDLService。该对象提供两个方法,一个是registerTestCall注冊一个aidl对象。通过该方法,AIDLTestActivity把本身实现的一个aidl对象AIDLActivity传到AIDLTestService。在AIDLTestService通过操作AIDLActivity这个aidl远端对象代理,使AIDLTestActivity弹出一个toast。完整样例见我上传的资源:

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

文章匆匆做,有欢迎大家讨论任何问题。

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

跨进程调用Service(AIDL Service)的更多相关文章

  1. Android中的跨进程调用技术AIDL

    什么是AIDL Android系统中的进程之间不能共享内存,因此,需要提供一些机制在不同进程之间进行数据通信. 为了使其他的应用程序也可以访问本应用程序提供的服务,Android系统采用了远程过程调用 ...

  2. Android四大组件应用系列5——使用AIDL实现跨进程调用Service

    一.问题描述 Android应用程序的四大组件中Activity.BroadcastReceiver.ContentProvider.Service都可以进行跨进程.在上一篇我们通过ContentPr ...

  3. Android菜鸟的成长笔记(21)——跨进程调用Service

    我们都知道在Android中的每一个应用是一个进程,其实每一个应用就相当于Linux系统里面的一个用户,进程和进程之间的通信其实就相当于用户和用户之间的通信,为了实现这种跨进程通信,Android提供 ...

  4. Android 跨应用调用Activity及Service

    如何调用另外一个app应用的activity或者service,本文提供一个验证可行的方法. 调用方法: Intent intent=new Intent("youActionName&qu ...

  5. Android 跨进程调用忽略权限

    Framework层: @Override    public StackInfo getStackInfo(int stackId) {        final int callingUid = ...

  6. Delphi---ShellExecute跨进程调用exe

    测试环境:Delphi7 + Win7 发起端 unit uRequest; interface uses Windows, Messages, SysUtils, Variants, Classes ...

  7. Android AIDL Service

    AIDL Service //跨进程调用Service    一个APK想调用另一个APK中的Service 如何实现AIDL //定义两个进程之间的通信接口 Demo1 传递简单数据 AidlSer ...

  8. 怎样用AIDL Service 传递复杂数据

    大家都知道在Android中通过AIDL可以跨进程调用Service中的数据,网上也有很多实例,但是大部分实例都是关于基本数据类型的远程调用,很少讲到复杂数据的调用,今天我用一个例子来演示一下怎样用A ...

  9. 【转载】Android Studio Service AIDL 详解

    公司产品之前IM这块存在很多问题,消息到达率低,加上协议上有些问题,丢消息频繁,所以需要重构IM,AIDL不能解决以上问题.好吧!那AIDL可以解决什么问题?什么是AIDL? 什么是AIDL? AID ...

随机推荐

  1. JS生成一个种子随机数(伪随机数)

    原文链接:https://geniuspeng.github.io/2016/09/12/js-random/ 最近有一个需求,需要生成一个随机数,但是又不能完全随机,就是说需要一个种子seed,se ...

  2. ios开发网络学习十一:NSURLSessionDataTask离线断点下载(断点续传)

    #import "ViewController.h" #define FileName @"121212.mp4" @interface ViewControl ...

  3. [Compose] 16. Apply multiple functors as arguments to a function (Applicatives)

    We find a couple of DOM nodes that may or may not exist and run a calculation on the page height usi ...

  4. php数学和时间常用函数有哪些(总结表)(看学习视频效率挺高的)(复习)

    php数学和时间常用函数有哪些(总结表)(看学习视频效率挺高的)(复习) 一.总结 一句话总结: 1.数学函数常用的6个:max().min().cell().floor().round().mt_r ...

  5. 学习鸟哥的Linux私房菜笔记(4)——文件

    一.检查文件 用ls -l以长模式查看文件的详细信息,包含当前目录的硬盘使用空间.文件类型.文件权限.硬连接数.文件拥有者.文件所属组.文件大小.更改时间.文件名称. 用file检查文件类型 由于li ...

  6. Scala入门到精通——第二十七节 Scala操纵XML

    本节主要内容 XML 字面量 XML内容提取 XML对象序列化及反序列化 XML文件读取与保存 XML模式匹配 1. XML 字面量 XML是一种很重要的半结构化数据表示方式,眼下大量的应用依赖于XM ...

  7. const常量用extern声明定义的问题(extern变量不能在使用类里初始化)

    test.h #ifndef TEST_H_ #define TEST_H //常量声明和定义采取这种方法即可 const int a = 20;  //不报错,因为const变量链接属性默认是内部链 ...

  8. AutoHotKey 的使用 —— 使用键盘调节 windows 声音

    AutoHotKey 下载地址 AutoHotkey Downloads 首先进行 AutoHotKey 的安装 编写如下 .ahk 文件(F10:打开关闭声音,F11:增加声音,F12:减少声音,当 ...

  9. for循环中setTimeout,var与let的不同

    先看下面两段代码 for (let i = 0; i < 5; i++) { setTimeout(function () { console.log(i) }, 2000) } for (va ...

  10. Android菜鸟的成长笔记(20)——IntentService

    前面介绍的Service在官方文档介绍中说Service存在着如下两个问题: 1.A Service is not a separate process. The Service object its ...