进程间通信的一个例子
2016-05-26 16:35
218 查看
概述
我们知道,在创建一个新进程的时候,AMS会通过IBinder的方式与Zygore通信,请求fork出一个新的进程,那么这个过程到底是怎样的呢,下面我来大概总结一下。
过程
先大概讲一下这个过程:Android Binder框架分为服务器接口、Binder驱动、以及客户端接口
服务器端接口:实际上是Binder类的对象,该对象一旦创建,内部则会启动一个隐藏线程,会接收Binder驱动发送的消息,收到消息后,会执行Binder对象中的onTransact()函数,并按照该函数的参数执行不同的服务器端代码。
Binder驱动:该对象也为Binder类的实例,客户端通过该对象访问远程服务。
客户端接口:获得Binder驱动,调用其transact()发送消息至服务器
接下来我们就结合上面的原理,从源码的角度分析下这个过程,我们以ActivityManagerProxy中启动Activity为例。
当发起一个新的进程的时候,我们通常会调用startActivity方法,这个方法是有服务端AMS在客户端的代理ActivityManagerProxy来执行的,我们看看代码
public int startActivity(IApplicationThread caller, String callingPackage, Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode, int startFlags, ProfilerInfo profilerInfo, Bundle options) throws RemoteException { Parcel data = Parcel.obtain(); Parcel reply = Parcel.obtain(); data.writeInterfaceToken(IActivityManager.descriptor); data.writeStrongBinder(caller != null ? caller.asBinder() : null); data.writeString(callingPackage); intent.writeToParcel(data, 0); data.writeString(resolvedType); data.writeStrongBinder(resultTo); data.writeString(resultWho); data.writeInt(requestCode); data.writeInt(startFlags); if (profilerInfo != null) { data.writeInt(1); profilerInfo.writeToParcel(data, Parcelable.PARCELABLE_WRITE_RETURN_VALUE); } else { data.writeInt(0); } if (options != null) { data.writeInt(1); options.writeToParcel(data, 0); } else { data.writeInt(0); } mRemote.transact(START_ACTIVITY_TRANSACTION, data, reply, 0); reply.readException(); int result = reply.readInt(); reply.recycle(); data.recycle(); return result; }
这里调用了mRemote对象的transact函数
相关文章推荐
- ndk的一些知识
- centos6.5 安装memcached 代理服务器(Magent)
- 方法一 Java如何判断线程池所有任务是否执行完毕
- 第二阶段工作01
- Qwerty78 Trip(组合数,规律,逆元)
- Kylin - 分析数据
- 微信分享到朋友圈,分享给朋友JS代码
- WebView自适应高度
- 餐巾计划问题[网络流24题之10]
- 【Zookeeper源码五】Zookeeper 集群版建立连接过程
- c++中不区分大小写比较字符串大小
- Solr之搭建Solr6.0服务并从Mysql上导入数据
- x101个LINQ 例子
- Maven Scope
- instanceof关键字
- Cygwin make 出错 permission denied
- Dynamics CRM Unified Service Desk 技术分享
- POJ1769 Minimizing maximizer(DP + 线段树)
- lldb调试使用python脚本问题总结
- Shell教程