深入分析AIDL原理

  • 博客分类:
  • Android

在上一篇文章(Service使用方式)中,介绍了Android进程间通信(IPC)的使用,并给出了一个示例。但并没有深入分析aidl是怎样可以做到进程间通信的,它的执行过程是怎样的?

这篇文章来分析IRemoteService.aidl的执行过程,并理解aidl是怎样跨进程通信的。

当我们创建IRemoteService.aidl文件时,IDE会为我们在gen目录中创建相应的文件。

Iremoteservice.java代码  
  1. /** This file is auto-generated.  DO NOT MODIFY.
  2. * Original file: F:\\workspace\\AndroidImprove\\src\\com\\example\\aidl\\IRemoteService.aidl
  3. */
  4. package com.example.aidl;
  5. public interface IRemoteService extends android.os.IInterface
  6. {
  7. /** Local-side IPC implementation stub class. */
  8. public static abstract class Stub extends android.os.Binder implements com.example.aidl.IRemoteService
  9. {
  10. private static final java.lang.String DESCRIPTOR = "com.example.aidl.IRemoteService";
  11. /** Construct the stub at attach it to the interface. */
  12. public Stub()
  13. {
  14. this.attachInterface(this, DESCRIPTOR);
  15. }
  16. /**
  17. * Cast an IBinder object into an com.example.aidl.IRemoteService interface,
  18. * generating a proxy if needed.
  19. */
  20. public static com.example.aidl.IRemoteService asInterface(android.os.IBinder obj)
  21. {
  22. if ((obj==null)) {
  23. return null;
  24. }
  25. android.os.IInterface iin = (android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR);
  26. if (((iin!=null)&&(iin instanceof com.example.aidl.IRemoteService))) {
  27. return ((com.example.aidl.IRemoteService)iin);
  28. }
  29. return new com.example.aidl.IRemoteService.Stub.Proxy(obj);
  30. }
  31. public android.os.IBinder asBinder()
  32. {
  33. return this;
  34. }
  35. @Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
  36. {
  37. switch (code)
  38. {
  39. case INTERFACE_TRANSACTION:
  40. {
  41. reply.writeString(DESCRIPTOR);
  42. return true;
  43. }
  44. case TRANSACTION_register:
  45. {
  46. data.enforceInterface(DESCRIPTOR);
  47. com.example.aidl.IRemoteCallback _arg0;
  48. _arg0 = com.example.aidl.IRemoteCallback.Stub.asInterface(data.readStrongBinder());
  49. this.register(_arg0);
  50. reply.writeNoException();
  51. return true;
  52. }
  53. case TRANSACTION_unregister:
  54. {
  55. data.enforceInterface(DESCRIPTOR);
  56. com.example.aidl.IRemoteCallback _arg0;
  57. _arg0 = com.example.aidl.IRemoteCallback.Stub.asInterface(data.readStrongBinder());
  58. this.unregister(_arg0);
  59. reply.writeNoException();
  60. return true;
  61. }
  62. case TRANSACTION_execute:
  63. {
  64. data.enforceInterface(DESCRIPTOR);
  65. this.execute();
  66. reply.writeNoException();
  67. return true;
  68. }
  69. case TRANSACTION_getStatus:
  70. {
  71. data.enforceInterface(DESCRIPTOR);
  72. java.lang.String _arg0;
  73. _arg0 = data.readString();
  74. int _result = this.getStatus(_arg0);
  75. reply.writeNoException();
  76. reply.writeInt(_result);
  77. return true;
  78. }
  79. }
  80. return super.onTransact(code, data, reply, flags);
  81. }
  82. private static class Proxy implements com.example.aidl.IRemoteService
  83. {
  84. private android.os.IBinder mRemote;
  85. Proxy(android.os.IBinder remote)
  86. {
  87. mRemote = remote;
  88. }
  89. public android.os.IBinder asBinder()
  90. {
  91. return mRemote;
  92. }
  93. public java.lang.String getInterfaceDescriptor()
  94. {
  95. return DESCRIPTOR;
  96. }
  97. //注册回调
  98. public void register(com.example.aidl.IRemoteCallback callback) throws android.os.RemoteException
  99. {
  100. android.os.Parcel _data = android.os.Parcel.obtain();
  101. android.os.Parcel _reply = android.os.Parcel.obtain();
  102. try {
  103. _data.writeInterfaceToken(DESCRIPTOR);
  104. _data.writeStrongBinder((((callback!=null))?(callback.asBinder()):(null)));
  105. mRemote.transact(Stub.TRANSACTION_register, _data, _reply, 0);
  106. _reply.readException();
  107. }
  108. finally {
  109. _reply.recycle();
  110. _data.recycle();
  111. }
  112. }
  113. //取消注册回调
  114. public void unregister(com.example.aidl.IRemoteCallback callback) throws android.os.RemoteException
  115. {
  116. android.os.Parcel _data = android.os.Parcel.obtain();
  117. android.os.Parcel _reply = android.os.Parcel.obtain();
  118. try {
  119. _data.writeInterfaceToken(DESCRIPTOR);
  120. _data.writeStrongBinder((((callback!=null))?(callback.asBinder()):(null)));
  121. mRemote.transact(Stub.TRANSACTION_unregister, _data, _reply, 0);
  122. _reply.readException();
  123. }
  124. finally {
  125. _reply.recycle();
  126. _data.recycle();
  127. }
  128. }
  129. //执行回调
  130. public void execute() throws android.os.RemoteException
  131. {
  132. android.os.Parcel _data = android.os.Parcel.obtain();
  133. android.os.Parcel _reply = android.os.Parcel.obtain();
  134. try {
  135. _data.writeInterfaceToken(DESCRIPTOR);
  136. mRemote.transact(Stub.TRANSACTION_execute, _data, _reply, 0);
  137. _reply.readException();
  138. }
  139. finally {
  140. _reply.recycle();
  141. _data.recycle();
  142. }
  143. }
  144. //获取状态
  145. public int getStatus(java.lang.String flag) throws android.os.RemoteException
  146. {
  147. android.os.Parcel _data = android.os.Parcel.obtain();
  148. android.os.Parcel _reply = android.os.Parcel.obtain();
  149. int _result;
  150. try {
  151. _data.writeInterfaceToken(DESCRIPTOR);
  152. _data.writeString(flag);
  153. mRemote.transact(Stub.TRANSACTION_getStatus, _data, _reply, 0);
  154. _reply.readException();
  155. _result = _reply.readInt();
  156. }
  157. finally {
  158. _reply.recycle();
  159. _data.recycle();
  160. }
  161. return _result;
  162. }
  163. }
  164. static final int TRANSACTION_register = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
  165. static final int TRANSACTION_unregister = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
  166. static final int TRANSACTION_execute = (android.os.IBinder.FIRST_CALL_TRANSACTION + 2);
  167. static final int TRANSACTION_getStatus = (android.os.IBinder.FIRST_CALL_TRANSACTION + 3);
  168. }
  169. //注册回调
  170. public void register(com.example.aidl.IRemoteCallback callback) throws android.os.RemoteException;
  171. //取消注册回调
  172. public void unregister(com.example.aidl.IRemoteCallback callback) throws android.os.RemoteException;
  173. //执行回调
  174. public void execute() throws android.os.RemoteException;
  175. //获取状态
  176. public int getStatus(java.lang.String flag) throws android.os.RemoteException;
  177. }

在ClientActivity绑定远程Service并建立连接时会调用ServiceConnection.onServiceConnected(ComponentName name, IBinder service)

Java代码  
  1. public void onServiceConnected(ComponentName name, IBinder service) {
  2. remoteService = IRemoteService.Stub.asInterface(service);
  3. //注册回调
  4. try {
  5. remoteService.register(remoteCallback);
  6. } catch (RemoteException e) {
  7. e.printStackTrace();
  8. }
  9. }

IBinder service是从RemoteService返回的IRemoteService.Stub iBinder,这个对象是Server应用进程中的对象。

IRemoteService.Stub.asInterface(service)在本地创建了一个代理

public static com.example.aidl.IRemoteService asInterface(android.os.IBinder obj)

{

if ((obj==null)) {

return null;

}

android.os.IInterface iin = (android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR);//这里肯定返回null

if (((iin!=null)&&(iin instanceof com.example.aidl.IRemoteService))) {

return ((com.example.aidl.IRemoteService)iin);

}

return new com.example.aidl.IRemoteService.Stub.Proxy(obj);//创建一个本地代理

}

当使用remoteService调用方法时,其实是调用了本地com.example.aidl.IRemoteService.Stub.Proxy对象的方法,从Proxy方法中可以看到,每个方法都执行了mRemote.transact(Stub.TRANSACTION_xxx, _data, _reply, 0);。

如:

Java代码  
  1. //获取状态
  2. public int getStatus(java.lang.String flag) throws android.os.RemoteException
  3. {
  4. android.os.Parcel _data = android.os.Parcel.obtain();
  5. android.os.Parcel _reply = android.os.Parcel.obtain();
  6. int _result;
  7. try {
  8. _data.writeInterfaceToken(DESCRIPTOR);
  9. _data.writeString(flag);
  10. mRemote.transact(Stub.TRANSACTION_getStatus, _data, _reply, 0);
  11. _reply.readException();
  12. _result = _reply.readInt();
  13. }
  14. finally {
  15. _reply.recycle();
  16. _data.recycle();
  17. }
  18. return _result;
  19. }

这一过程是把Client端的参数转换成Parcel(_data)传递到Server端,而在Server端又会把返回数据保存到_reply中,这就形成了一次交互。

mRemote是远程对象,transact方法会执行onTransact方法

Java代码  
  1. public final boolean transact(int code, Parcel data, Parcel reply,
  2. int flags) throws RemoteException {
  3. if (Config.LOGV) Log.v("Binder", "Transact: " + code + " to " + this);
  4. if (data != null) {
  5. data.setDataPosition(0);
  6. }
  7. boolean r = onTransact(code, data, reply, flags);
  8. if (reply != null) {
  9. reply.setDataPosition(0);
  10. }
  11. return r;
  12. }

这样就会执行远程的onTransact方法,

Java代码  
  1. case TRANSACTION_getStatus:
  2. {
  3. data.enforceInterface(DESCRIPTOR);
  4. java.lang.String _arg0;
  5. _arg0 = data.readString();
  6. int _result = this.getStatus(_arg0);
  7. reply.writeNoException();
  8. reply.writeInt(_result);
  9. return true;
  10. }

注意 int _result = this.getStatus(_arg0);,这就调用了Server端的getStatus(String flag),并把返回结果写到Client端的代理Proxy对象的_reply中

到此,aidl通信过程就完成了。

PS: aidl通信有点复杂,但仔细分析并不是很难

android AIDL IPC深入分析相关推荐

  1. Android的IPC机制(一)——AIDL的使用

    综述 IPC(interprocess communication)是指进程间通信,也就是在两个进程间进行数据交互.不同的操作系统都有他们自己的一套IPC机制.例如在Linux操作系统中可以通过管道. ...

  2. (转载)你真的理解Android AIDL中的in,out,inout么?

    前言 这其实是一个很小的知识点,大部分人在使用AIDL的过程中也基本没有因为这个出现过错误,正因为它小,所以在大部分的网上关于AIDL的文章中,它都被忽视了--或者并没有,但所占篇幅甚小,且基本上都是 ...

  3. 范例解析:学习Android的IPC主板模式

    一.认识Android的IPC主板模式 系统架构设计最关键的任务就是组合(或称整合),而且最好是能与众不同.深具创新性组合.Android就擅用了主板模式,以通用性接口实践跨进程的IPC通信机制.由于 ...

  4. Android AIDL使用介绍(3) 浅说AIDL背后的Binder

    1.背景 在前面的博客中,我们已经学会了使用AIDL进行跨进程通信,AIDL的使用比较简单,可实际上跨进程通信是一个相当复杂的过程,例如进程A是怎么找到进程B的,如果有一个进程C冒充进程A,进程B又该 ...

  5. android主板接口定义,范例解析:学习Android的IPC主板模式

    一.认识Android的IPC主板模式 系统架构设计最关键的任务就是组合(或称整合),而且最好是能与众不同.深具创新性组合.Android就擅用了主板模式,以通用性接口实践跨进程的IPC通信机制.由于 ...

  6. Android AIDL使用详解

    一.概述 AIDL 意思即 Android Interface Definition Language,翻译过来就是Android接口定义语言,是用于定义服务器和客户端通信接口的一种描述语言,可以拿来 ...

  7. Android的IPC

    一.什么是Android的IPC? IPC的使用场合? 1.IPC是Inter-Process Communication的缩写,含义是进程间通信或跨进程通信,指的是两个进程间进行数据交换的过程. 2 ...

  8. Android随笔-IPC

    概述 IPC全程Inter-Process Communication,跨进程通信,指的是两个进程之间进行数据交换的过程.IPC并不是Android独有的,每一个操作系统需要有相应的IPC机制,比如W ...

  9. 猿创征文 | Android AIDL 学习笔记——学以致用

    文章目录 Android AIDL 跨进程通信 AIDL文件 AIDL语法 数据类型 关键字 引用 指定方法ID 总结 实现接口 获取AIDL对象 捕获异常 创建Parcelable对象 带Bundl ...

最新文章

  1. 用easyui动态创建一个对话框
  2. asp.net实现C#代码加亮显示
  3. Android深入四大组件(八)广播的注册、发送和接收过程
  4. led大屏按实际尺寸设计画面_年会活动要用LED大屏还是投影?专业行家都是看这些数据。...
  5. android自带中文字体,Android更换系统默认显示的字体使用自定义字体
  6. Nginx内核参数相关的优化设定
  7. 【TypeScript系列教程07】变量声明
  8. 去除标题_资深运营导师-云中教你轻松写标题
  9. eclispe快捷键
  10. 云图说|玩转华为HiLens之端云协同AI开发
  11. 奇妙的等式 精妙的证明(二)
  12. MP(Multi-Link PPP)原理和实验
  13. Coding and Paper Letter(五十九)
  14. 【第一期】电商分布式前沿springboot接口服务之配置-Array-专题视频课程
  15. 开启docker远程访问
  16. 苹果手机语音备忘录在哪_真没想到!苹果手机还自带语音记录,按下这个按钮,语音秒变文字...
  17. 神经网络与深度学习(邱锡鹏)
  18. 化工机械基础试题及答案
  19. 如何使用SFC / SCANNOW修复Windows系统文件
  20. 智引万物论剑AI,商汤科技欲打造颠覆式创新引擎

热门文章

  1. python异常值删除_python删除有异常值
  2. 机器人最大的人类士人禾力积木_开化县华埠镇中心小学:积木机器人好玩儿~~...
  3. linux nat 端口,linux – iptables nat只是端口25?
  4. java timer.schedule如何控制执行次数_Java 分布式任务调度平台:PowerJob 快速开始+配置详解...
  5. java栈 迷宫_利用栈实现迷宫的求解
  6. ftl模板导出excel_freemarker导出复杂Excel
  7. 计算机 大学活动 游戏,朝花夕拾”——中国矿业大学计算机学院积极举办“那些年我们一起玩过的游戏”活动...
  8. 【camera】自动泊车-视觉车位检测相关资料汇总(论文、数据集、源代码、相关博客、演示demo)(1)
  9. Linux那些事儿之我是Sysfs(1)sysfs初探
  10. C语言实现RC4序列密码