FW-Binder机制
问题1 应用是怎么启用Binder机制
1 什么时候支持Binder机制


Activity.oncreate()应用在启动第一个activity时,getSystemService就能拿到系统服务,然后调用系统服务,这里面就用到了binder机制,再往早些,再Application.onCreate()里也用到了Binder机制,说明启用Binder机制的时机更早,如上图应用进程在创建好之后,第一个跨进程通信是和AMS通信的,所以Binder机制启动得更早,应该是在zygote启动应用进程的环节,根据源码得出是在启动应用进程之后的初始化里做的
2 怎么启用Binder机制

问题2 Binder对象跨进程传递的原理
考察点:


1 在parcel中传输,首先通过writeStrongBinder写入parcel中,然后通过readStrongBinder在目标进程中读取出来。
2 Binder对象如何写入parcel中?在native层封装了flat_binder_object对象(binder缓冲区,数组中保存flat_binder_object偏移,到了目标进程中parcel根据偏移还原出flat_binder_object),保存到了native层parcel中,parcel对象到了binder驱动层,binder驱动取出flat_binder_object,创建binder_ref,将binder实体对象转换为代理对象
3 binder_ref在目标进程会对应handle,handle往上传会对应BpBinder(native层Binder的对象),BpBinder再往上到BinderProxy到业务层的Proxy(native层的Binder对象转换为Java层的Binder对象)