首页 > 移动开发 > ANDROID > Android-Binder机制
作者:erki_stwee
链接:https://www.jianshu.com/p/225ccd39a667
来源:简书
IPC原理
在描述Binder之前先简单的了解一下IPC原理;
从进程角度来看IPC机制
每个Android的进程(app),只能运行在自己进程所拥有的虚拟地址空间。虚拟地址空间又分为用户空间和内核空间,对于用户空间是不能共享的。内核空间是可以共享的。Client端向Service端进程通信,利用的就是可共享内核内存空间来完成底层底层通信工作的。
什么是Binder
简单来说Binder是一种跨进程的通信方式;
从Android
Framework层来说,Binder是ServiceManager连接各种Manager和相应ManagerService的桥梁;
从Android引用层来说,它是C/S架构的媒介;
工作过程
Binder工作机制
客户端发起远程请求调用对应的方法时,
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
首先创建该方法所需的输入性Parcel对象_data,输出性Parcel对象_reply和返回值对象(如果有返回值)
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(anInt);
_data.writeLong(aLong);
_data.writeInt(((aBoolean)?(1):(0)));
_data.writeFloat(aFloat);
_data.writeDouble(aDouble);
_data.writeString(aString);
然后把该方法的参数信息写入到_data中(如果有参数)
mRemote.transact(Stub.TRANSACTION_basicTypes, _data, _reply, 0);
紧接着调用transact方法来发起RPC(远程过程调用)请求,同时当前线程被挂起;服务端的onTransact方法会被调用,直到RPC过程返回后,当前线程继续执行,并从_reply中取出RPC过程的返回结果;最后返回_reply中的数据;
/**
* Default implementation is a stub that returns false. You will want
* to override this to do the appropriate unmarshalling of transactions.
*
*
If you want to call this, call transact().
*/
protected boolean onTransact(int code, Parcel data, Parcel reply,
int flags) throws RemoteException {
}
该方法运行在服务端的Binder线程池中,当客户端发起跨进程请求时,远程请求会通过系统底层封装后交由此方法处理;如果返回false,客户端的请求会失败,我们可以利用这个特性来做权限校验来保证安全性。
需要特别注意:无论Binder方法是否耗时都应该采用同步的方式去实现,因为它已经运行在一个线程中了。
使用场景
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/20484604/viewspace-3144484/ ,如需转载,请注明出处,否则将追究法律责任。