Parcel在binder通信readStrongBinder和writeStrongBinder

Binder IPC通信中,Binder是通信的媒介,Parcel是通信的內容。远程调用过程中,其参数都被打包成Parcel的形式来传递。

在IPC通信的Proxy端,我们经常可以看到下面类似的代码,一些参数都会打包到Parcel中。看下面的data和reply。

public void publishService(IBinder token,
            Intent intent, IBinder service) throws RemoteException {
        Parcel data = Parcel.obtain();
        Parcel reply = Parcel.obtain();
        data.writeInterfaceToken(IActivityManager.descriptor);
        data.writeStrongBinder(token);
        intent.writeToParcel(data, 0);
        data.writeStrongBinder(service);
        mRemote.transact(PUBLISH_SERVICE_TRANSACTION, data, reply, 0);
        reply.readException();
        data.recycle();
        reply.recycle();
    }

在IPC通信里,android是通过Parcel类的成员函数writeStrongBinder()向Parcel写入IBinder,再通过mRemote发送出去。

2.writeStrongBinder()

接下來就看一下writeStrongBinder()这个函数里做了什么。
/frameworks/base/core/java/android/os/Parcel.java

public final class Parcel {
    …
    /**
     * Write an object into the parcel at the current dataPosition(),
     * growing dataCapacity() if needed.
     */
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,Binder进程间通信(IPC)机制可以实现跨进程调用原进程的方法。 在Binder IPC中,存在客户端和服务端两个角色。客户端通过Binder代理(Proxy)向服务端发送请求,而服务端通过Binder存根(Stub)接收请求并执行相应的方法。 当客户端向服务端发送请求时,可以通过Binder IPC机制实现跨进程调用原进程的方法。具体步骤如下: 1. 客户端调用:客户端通过Binder代理(Proxy)调用服务端的方法,并传递相应的参数。 2. 数据传输:客户端将方法调用请求和参数打包成一个Parcel对象,并通过Binder IPC机制将该Parcel对象发送给服务端。 3. 服务端接收:服务端在接收到客户端的请求后,通过Binder存根(Stub)解析接收到的Parcel对象,并提取出方法调用请求和参数。 4. 方法执行:服务端根据解析得到的方法调用请求和参数,执行相应的方法。 5. 返回结果:在方法执行完毕后,服务端将返回的结果打包成一个Parcel对象,并通过Binder IPC机制将该Parcel对象发送回客户端。 6. 客户端接收:客户端在接收到服务端返回的结果后,通过Binder代理(Proxy)解析接收到的Parcel对象,提取出返回结果。 需要注意的是,跨进程调用原进程方法需要在服务端实现相应的方法,并且确保方法的调用参数和返回值能够正确地在进程间进行序列化和反序列化。此外,由于跨进程调用涉及到进程间的通信,因此需要注意线程安全和数据一致性的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值