- 浏览: 591482 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (154)
- java (31)
- Struts (3)
- Hibernate (8)
- Spring (6)
- JS (14)
- DWR (1)
- Oracle (1)
- SQL Server (0)
- JSF (0)
- JPA (0)
- Grails (3)
- Ajax (1)
- Extjs (0)
- jQuery (4)
- MySQL (2)
- 正则表达式 (2)
- 其他 (6)
- 计算机应用 (7)
- Groovy (8)
- Google (1)
- Web (2)
- MongoDB Java Driver (1)
- mongodb (1)
- Linux (3)
- HTML (1)
- Tomcat (3)
- Eclipse (2)
- JSP (1)
- 缓存 (1)
- 动态 (1)
- 问题与解决 (3)
- HY (1)
- Android (35)
- LBS (1)
最新评论
-
u012136165:
list 方法:纠正:[2,5].add(1,9) ...
Groovy的list和map -
bruce.yuan:
误人子弟的文章。已经看到N个人转了这个帖子,这要贻害多少新人啊 ...
理解并解决GBK转UTF-8奇数中文乱码 -
思念-悲伤:
特意登录上来,感谢下!
理解String的compareTo()方法返回值 -
bo_hai:
总结的好,有效。
理解String的compareTo()方法返回值 -
u012678420:
在onCreate方法中获取某个View的宽度和高度
深入分析AIDL原理
- 博客分类:
- Android
在上一篇文章(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通信有点复杂,但仔细分析并不是很难
发表评论
-
Service使用方式
2011-11-13 15:23 6025使用Service的场合: ... -
在onCreate方法中获取某个View的宽度和高度
2011-10-08 14:54 14418有时候需要在onCreate方法中知道某个View组件的宽度和 ... -
不加载图片,仅仅获取图片文件分辨率
2011-10-08 11:09 3928当从SD卡中获取图片时,如果图片太大,加载会出现溢出异常。因此 ... -
图片移动和缩放
2011-09-29 22:15 2103import android.graphics.Matr ... -
定时任务
2011-09-28 00:53 1485定时任务有执行一次和重复执行两种 public cla ... -
几种创建LayoutInflater对象的方法
2011-09-28 00:51 1838LayoutInflater layoutInflate ... -
判断网络类型及是否有网络
2011-09-28 00:49 2740判断当前是否有可用网络 public boolean ... -
设置全屏
2011-09-26 23:40 18891.编程方式 public void setFullS ... -
dip与px单位之间转换
2011-09-25 10:11 1611//转换dip为px public static in ... -
屏幕尺寸(高、宽)
2011-09-25 10:11 1865Display display = getWindowM ... -
判断手机外存(SD卡)状态
2011-09-25 10:07 2136public int getExternalStorag ... -
SQLite数据类型
2011-09-25 10:03 9317一般数据采用的固 ... -
使用WebView实现腾讯微博OAuth登录
2011-09-24 21:11 4851接着上一篇文章"使用WebView实现新浪微博OAu ... -
使用WebView实现新浪微博OAuth登录
2011-09-20 12:18 8466#新浪官方下载SDK(weibo4android) h ... -
高效率下载图片——防止内存溢出
2011-09-18 13:09 2177在应用中经常需要下载很多的图片,因此,写好图片下载部分的代码非 ... -
PopupWindow使用
2011-09-16 14:05 1765使用PopupWindow来显示一个浮层 点击按钮后弹出 ... -
Android通知
2011-09-07 17:56 1887private void showNotification() ... -
AlarmManager.RTC和ELAPSED_REALTIME的区别
2011-09-04 17:33 30032AlarmManager.RTC,硬件闹钟,不唤醒手机( ... -
开机自启动
2011-09-04 13:55 1619经常会有这样的需求,当手机开机后立刻启动一个服务或者一个活动。 ... -
Android虚拟机(DVM)内存分配——内存溢出问题
2011-09-03 17:19 9867大家都知道Android的上层应用是基于 Dalvik ...
相关推荐
8.4.7 深入分析Surface的总结 8.5 SurfaceFlinger分析 8.5.1 SurfaceFlinger的诞生 8.5.2 SF工作线程分析 8.5.3 Transaction分析 8.5.4 关于SurfaceFlinger的总结 8.6 拓展思考 8.6.1 Surface系统的CB对象分析 8.6.2...
《深入理解android:卷1》是一本以情景方式对android的源代码进行深入分析的书。内容广泛,以对framework层的分析为主,兼顾native层和application层;分析深入,每一部分源代 码的分析都力求透彻;针对性强,...
8.4.7 深入分析Surface的总结 / 353 8.5 SurfaceFlinger分析 / 353 8.5.1 SurfaceFlinger的诞生 / 354 8.5.2 SF工作线程分析 / 359 8.5.3 Transaction分析 / 368 8.5.4 关于SurfaceFlinger的总结 / 376 8.6 拓展思考...
第7章 Android动画深入分析 265 7.1 View动画 265 7.1.1 View动画的种类 265 7.1.2 自定义View动画 270 7.1.3 帧动画 272 7.2 View动画的特殊使用场景 273 7.2.1 LayoutAnimation 273 7.2.2 Activity的切换...
第7章 Android动画深入分析 / 265 7.1 View动画 / 265 7.1.1 View动画的种类 / 265 7.1.2 自定义View动画 / 270 7.1.3 帧动画 / 272 7.2 View动画的特殊使用场景 / 273 7.2.1 LayoutAnimation / 273 ...
258 6.2.9 Clip Drawable / 260 6.3 自定义Drawable / 262 第7章 Android动画深入分析 / 265 7.1 View动画 / 265 7.1.1 View动画的种类 / 265 7.1.2 自定义View动画 / 270 7.1.3 帧动画 / 272 7.2 View动画的...
3、通过linux键盘驱动案例的讲解,分析盗号木马的原理及其实现方式。 4、恶意软件发展速度的确一日千里,安全软件也要与时俱进,世面上的手机病毒已经具有了可以杀掉安全软件的功能, 通过多进程互相守护案例,讲述...
【Android系统原理与开发要点详解】/底层 应用 框架 Android核心分析28篇,强烈推荐android初学者,android进阶者看看这个系列教程 Android应用开发者指南:性能优化 android开发教程合集(推荐新手看下这一季教程)...
本书作者从事多年一线开发和培训,讲解知识点力求细致,深入浅出 目 录 前言 第1章 Android操作系统概述 1 1.1 Android历史介绍 1 1.2 Android架构 1 1.3 Android平台介绍 2 1.4 现有智能手机操作系统比较 4 ...