深入分析AIDL原理
在上一篇文章(Service使用方式)中,介绍了Android进程间通信(IPC)的使用,并给出了一个示例。但并没有深入分析aidl是怎样可以做到进程间通信的,它的执行过程是怎样的?
这篇文章来分析IRemoteService.aidl的执行过程,并理解aidl是怎样跨进程通信的。
当我们创建IRemoteService.aidl文件时,IDE会为我们在gen目录中创建相应的文件。
- /** This file is auto-generated. DO NOT MODIFY.
- * Original file: F:\\workspace\\AndroidImprove\\src\\com\\example\\aidl\\IRemoteService.aidl
- */
- package com.example.aidl;
- public interface IRemoteService extends android.os.IInterface
- {
- /** Local-side IPC implementation stub class. */
- public static abstract class Stub extends android.os.Binder implements com.example.aidl.IRemoteService
- {
- private static final java.lang.String DESCRIPTOR = "com.example.aidl.IRemoteService";
- /** Construct the stub at attach it to the interface. */
- public Stub()
- {
- this.attachInterface(this, DESCRIPTOR);
- }
- /**
- * Cast an IBinder object into an com.example.aidl.IRemoteService interface,
- * generating a proxy if needed.
- */
- 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);
- if (((iin!=null)&&(iin instanceof com.example.aidl.IRemoteService))) {
- return ((com.example.aidl.IRemoteService)iin);
- }
- return new com.example.aidl.IRemoteService.Stub.Proxy(obj);
- }
- public android.os.IBinder asBinder()
- {
- return this;
- }
- @Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
- {
- switch (code)
- {
- case INTERFACE_TRANSACTION:
- {
- reply.writeString(DESCRIPTOR);
- return true;
- }
- case TRANSACTION_register:
- {
- data.enforceInterface(DESCRIPTOR);
- com.example.aidl.IRemoteCallback _arg0;
- _arg0 = com.example.aidl.IRemoteCallback.Stub.asInterface(data.readStrongBinder());
- this.register(_arg0);
- reply.writeNoException();
- return true;
- }
- case TRANSACTION_unregister:
- {
- data.enforceInterface(DESCRIPTOR);
- com.example.aidl.IRemoteCallback _arg0;
- _arg0 = com.example.aidl.IRemoteCallback.Stub.asInterface(data.readStrongBinder());
- this.unregister(_arg0);
- reply.writeNoException();
- return true;
- }
- case TRANSACTION_execute:
- {
- data.enforceInterface(DESCRIPTOR);
- this.execute();
- reply.writeNoException();
- return true;
- }
- case TRANSACTION_getStatus:
- {
- data.enforceInterface(DESCRIPTOR);
- java.lang.String _arg0;
- _arg0 = data.readString();
- int _result = this.getStatus(_arg0);
- reply.writeNoException();
- reply.writeInt(_result);
- return true;
- }
- }
- return super.onTransact(code, data, reply, flags);
- }
- private static class Proxy implements com.example.aidl.IRemoteService
- {
- private android.os.IBinder mRemote;
- Proxy(android.os.IBinder remote)
- {
- mRemote = remote;
- }
- public android.os.IBinder asBinder()
- {
- return mRemote;
- }
- public java.lang.String getInterfaceDescriptor()
- {
- return DESCRIPTOR;
- }
- //注册回调
- public void register(com.example.aidl.IRemoteCallback callback) throws android.os.RemoteException
- {
- android.os.Parcel _data = android.os.Parcel.obtain();
- android.os.Parcel _reply = android.os.Parcel.obtain();
- try {
- _data.writeInterfaceToken(DESCRIPTOR);
- _data.writeStrongBinder((((callback!=null))?(callback.asBinder()):(null)));
- mRemote.transact(Stub.TRANSACTION_register, _data, _reply, 0);
- _reply.readException();
- }
- finally {
- _reply.recycle();
- _data.recycle();
- }
- }
- //取消注册回调
- public void unregister(com.example.aidl.IRemoteCallback callback) throws android.os.RemoteException
- {
- android.os.Parcel _data = android.os.Parcel.obtain();
- android.os.Parcel _reply = android.os.Parcel.obtain();
- try {
- _data.writeInterfaceToken(DESCRIPTOR);
- _data.writeStrongBinder((((callback!=null))?(callback.asBinder()):(null)));
- mRemote.transact(Stub.TRANSACTION_unregister, _data, _reply, 0);
- _reply.readException();
- }
- finally {
- _reply.recycle();
- _data.recycle();
- }
- }
- //执行回调
- public void execute() throws android.os.RemoteException
- {
- android.os.Parcel _data = android.os.Parcel.obtain();
- android.os.Parcel _reply = android.os.Parcel.obtain();
- try {
- _data.writeInterfaceToken(DESCRIPTOR);
- mRemote.transact(Stub.TRANSACTION_execute, _data, _reply, 0);
- _reply.readException();
- }
- finally {
- _reply.recycle();
- _data.recycle();
- }
- }
- //获取状态
- public int getStatus(java.lang.String flag) throws android.os.RemoteException
- {
- android.os.Parcel _data = android.os.Parcel.obtain();
- android.os.Parcel _reply = android.os.Parcel.obtain();
- int _result;
- try {
- _data.writeInterfaceToken(DESCRIPTOR);
- _data.writeString(flag);
- mRemote.transact(Stub.TRANSACTION_getStatus, _data, _reply, 0);
- _reply.readException();
- _result = _reply.readInt();
- }
- finally {
- _reply.recycle();
- _data.recycle();
- }
- return _result;
- }
- }
- static final int TRANSACTION_register = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
- static final int TRANSACTION_unregister = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
- static final int TRANSACTION_execute = (android.os.IBinder.FIRST_CALL_TRANSACTION + 2);
- static final int TRANSACTION_getStatus = (android.os.IBinder.FIRST_CALL_TRANSACTION + 3);
- }
- //注册回调
- public void register(com.example.aidl.IRemoteCallback callback) throws android.os.RemoteException;
- //取消注册回调
- public void unregister(com.example.aidl.IRemoteCallback callback) throws android.os.RemoteException;
- //执行回调
- public void execute() throws android.os.RemoteException;
- //获取状态
- public int getStatus(java.lang.String flag) throws android.os.RemoteException;
- }
在ClientActivity绑定远程Service并建立连接时会调用ServiceConnection.onServiceConnected(ComponentName name, IBinder service)
- public void onServiceConnected(ComponentName name, IBinder service) {
- remoteService = IRemoteService.Stub.asInterface(service);
- //注册回调
- try {
- remoteService.register(remoteCallback);
- } catch (RemoteException e) {
- e.printStackTrace();
- }
- }
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);。
如:
- //获取状态
- public int getStatus(java.lang.String flag) throws android.os.RemoteException
- {
- android.os.Parcel _data = android.os.Parcel.obtain();
- android.os.Parcel _reply = android.os.Parcel.obtain();
- int _result;
- try {
- _data.writeInterfaceToken(DESCRIPTOR);
- _data.writeString(flag);
- mRemote.transact(Stub.TRANSACTION_getStatus, _data, _reply, 0);
- _reply.readException();
- _result = _reply.readInt();
- }
- finally {
- _reply.recycle();
- _data.recycle();
- }
- return _result;
- }
这一过程是把Client端的参数转换成Parcel(_data)传递到Server端,而在Server端又会把返回数据保存到_reply中,这就形成了一次交互。
mRemote是远程对象,transact方法会执行onTransact方法
- public final boolean transact(int code, Parcel data, Parcel reply,
- int flags) throws RemoteException {
- if (Config.LOGV) Log.v("Binder", "Transact: " + code + " to " + this);
- if (data != null) {
- data.setDataPosition(0);
- }
- boolean r = onTransact(code, data, reply, flags);
- if (reply != null) {
- reply.setDataPosition(0);
- }
- return r;
- }
这样就会执行远程的onTransact方法,
- case TRANSACTION_getStatus:
- {
- data.enforceInterface(DESCRIPTOR);
- java.lang.String _arg0;
- _arg0 = data.readString();
- int _result = this.getStatus(_arg0);
- reply.writeNoException();
- reply.writeInt(_result);
- return true;
- }
注意 int _result = this.getStatus(_arg0);,这就调用了Server端的getStatus(String flag),并把返回结果写到Client端的代理Proxy对象的_reply中
到此,aidl通信过程就完成了。
PS: aidl通信有点复杂,但仔细分析并不是很难
深入分析AIDL原理相关推荐
- 插件化知识储备-Binder和AIDL原理
前言 插件化技术火热已久,为什么会有插件化,时势造英雄吧,随着移动互联网的快速发展,业务的飞速增长,如何在有限时间给用户提供高质量的APP,当线上出现各种BUG,如何快速修复并发布上线,插件化的意义也 ...
- Android10.0 Binder通信原理(十)-AIDL原理分析-Proxy-Stub设计模式
1.概述 上一节我们写了一个AIDL的示例,实现了两个应用之间的通信,这一节我们就来一起探讨下AIDL是如何生效的. 2.什么是AIDL AIDL:Android Interface Definiti ...
- java虚拟机线程调优与底层原理分析_啃碎并发(七):深入分析Synchronized原理...
原标题:啃碎并发(七):深入分析Synchronized原理 前言 记得开始学习Java的时候,一遇到多线程情况就使用synchronized,相对于当时的我们来说synchronized是这么的神奇 ...
- (转载)深入分析HDFS原理及读写流程
一.架构体系 1.1.什么是HDFS? HDFS即Hadoop Distributed File System的简称,采用Master/Slave主从结构模型来管理数据.在设计上采用了分而治之的思想, ...
- Android探索之旅 | AIDL原理和实例讲解
前言 为使应用程序之间能够彼此通信,Android提供了IPC (Inter Process Communication,进程间通信)的一种独特实现: AIDL (Android Interface ...
- 理解AIDL原理以及系统生成的源码
工具好用,但是会有一个缺点,就是会让你不得不依赖说明书.对于我这个记忆力很差的人来说,每次使用工具都要去翻说明书是一件极其痛苦的事情,而且还有很多坑会忘记怎么填.基于此,我很多时候会觉得不如自己去理解 ...
- 从一个例子开始分析AIDL原理
上一个项目(下载中心)使用到了AIDL相关的技术,趁现在项目不是特别繁忙,总结一下.首先第一个问题,AIDL是个啥东西?它的全称叫 Android Interface Definition Langu ...
- 深入分析Synchronized原理(阿里面试题)
我们学习Java的时候,一遇到多线程情况就使用synchronized,相对于当时的我们来说synchronized是这么的神奇而又强大,那个时候我们赋予它一个名字"同步",也成为 ...
- IPC、Binder及AIDL原理机制
一.IPC介绍 IPC是Inter-Process Communication的缩写,含义是进程间通信或者跨进程通信,是指两个进程之间进行数据交换的过程.那么为什么需要开启多进程呢?原因有二,其一是一 ...
- Android IPC 之 AIDL 原理
在上篇文章中我们使用 AIDL 完成了进程之间的通信,下面我们分析一下 AIDL 文件的实现类 aidl 文件如下: /*** 连接服务*/ interface IConnectionService ...
最新文章
- Spring Boot自动配置原理分析
- ZOJ 3827 Information Entropy(数学题 牡丹江现场赛)
- [翻译] Fast Image Cache
- 新手入门指导:Vue 2.0 的建议学习顺序
- 全网最细Docker安装Minio,填满最新版大坑(强烈推荐收藏)
- SAP License:如何获取采购订单一次性供应商的地址信息
- c++成员声明中的非法限定名_new 一个对象有哪两个过程?很多人在面试中都问住了...
- 易位构词EOJ3451【字符串】【思维题】【模拟】
- android 圆点指示器,ViewPager加上小圆点指示器效果
- 此计算机屏保怎么取消,如何取消屏幕保护
- csdn怎么查看自己写的文章
- 从VLP-16讲起的激光雷达原理(一)概念篇
- rs485的1:n计算机连接和n:n的主从plc通讯,富士人机界面与GE90-70 PLC N:1通讯在轧钢生产线的应用...
- 新任项目主管如何带好IT团队?
- 天眼查是怎么获得企业工商信息的?
- VBA每日一练(10)range.end() 或 cells().end() 用法,偏移 且 查找非空单元格,取最大最小行列数
- React Native 填坑记录01
- [数据集][VOC]老鼠数据集voc格式3001张
- 技术寡头争霸传之:控制开源工具,就控制了整个生态
- Java Web 网络商城案例演示一、(环境搭建)
热门文章
- STM32H7+USB3300+SD作为U盘速度慢的问题
- 微信视频提取及接收文件路径
- 计算机主机显示接口,一文了解电脑视频接口 看完彻底明白了
- 进行日常记账后,怎样导出表格
- 软件概要设计的过程和内容
- linux拷贝安装包到另一个目录下,把windows下的压缩包放到Linux目录下去
- 《机器学习-原理、算法与应用》出版了
- 精度LiDAR-Monocular Visual Odometry
- 某度起诉“文库下载神器”,索赔300万
- Qt5 与OpenCV4教程一:Qt5.12安装与OpenCV4.5.0配置