Android Binder机制 defaultServiceManager() 的实现

defaultServiceManager()返回的是C++层的IServiceManager对象

获取IServiceManager对象的目的是为了和ServiceManager进程进行通信

如:Server要通过IServiceManager对象发送请求指令注册到ServiceManager进程中,Client要通过IServiceManager对象发送请求来获取Server对象

ServiceManager进程是一个守护进程

对于一个server而言,它都会存在一个“远程BpBinder对象”和“本地BBinder对象”

远程BpBinder对象的作用是和Binder驱动进行交互,具体的方式是:当Server要向Binder发起事物请求时,会调用BpBinder的transact()接口,而该接口会调用IPCThreadState::transact()接口,通过IPCThreadState类和Binder驱动交互,并且BpBinder在Binder驱动中的Binder引用描述会被保存到ProcessState类的mHandleToObject矢量缓冲数组中。

本地BBinder是Server响应Client请求的类,当Client有请求发送给Server时,都会调用到BBinder的onTransact()函数,而每个Server都会覆盖onTransact()函数,这样,每个Server就可以在onTransact()中根据自己的情况来对请求进行处理。

原文地址:https://www.cnblogs.com/chen-cai/p/9642724.html