Android Binder机制原理java层系列三(有图有代码很详细)

该篇文章主要是根据上一篇Android Binder机制原理java层系列二(有图有代码很详细)中getService和addService中关于一些细节引申出来的内容!
在看ServiceManager过程中
1.代理端的ServiceManagerProxy的get/add
2.真正服务端的ServiceManagerNative的get/add

class ServiceManagerProxy implements IServiceManager {
    ....
    public IBinder asBinder() {
        return mRemote;
    }
    ....
    public IBinder getService(String name) throws RemoteException {
        Parcel data = Parcel.obtain();
        Parcel reply = Parcel.obtain();
        data.writeInterfaceToken(IServiceManager.descriptor);
        data.writeString(name);
        mRemote.transact(GET_SERVICE_TRANSACTION, data, reply, 0);
        IBinder binder = reply.readStrongBinder();
        reply.recycle();
        data.recycle();
        return binder;
    }
    
     public void addService(String name, IBinder service, boolean allowIsolated)
            throws RemoteException {
        Parcel data = Parcel.obtain();
        Parcel reply = Parcel.obtain();
        data.writeInterfaceToken(IServiceManager.descriptor);
        data.writeString(name);
        data.writeStrongBinder(service);
        data.writeInt(allowIsolated ? 1 : 0);
        mRemote.transact(ADD_SERVICE_TRANSACTION, data, reply, 0);
        reply.recycle();
        data.recycle();
    }
    ....
    private IBinder mRemote;
}
public abstract class ServiceManagerNative extends Binder implements IServiceManager
{
    ...
    public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
    {
        try {
            switch (code) {
            case IServiceManager.GET_SERVICE_TRANSACTION: {
                data.enforceInterface(IServiceManager.descriptor);
                String name = data.readString();
                IBinder service = getService(name);
                reply.writeStrongBinder(service);
                return true;
            }
            ....
            case IServiceManager.ADD_SERVICE_TRANSACTION: {
                data.enforceInterface(IServiceManager.descriptor);
                String name = data.readString();
                IBinder service = data.readStrongBinder();
                boolean allowIsolated = data.readInt() != 0;
                addService(name, service, allowIsolated);
                return true;
            }    
            ....
            }
        } catch (RemoteException e) {
        }
        return false;
    }
    ....
}

下面的两个方法,不知道大家有没有研究过,该篇博客就从这里入手研究

reply.readStrongBinder();
data.writeStrongBinder(service);

Parcel是用来跨进程传递数据的,主要有java层的Parcel,native层的Parcel,它们都是对应的
需要注意有这几个和parcel相关的文件

frameworks\base\core\java\android\os\Parcel.java
frameworks\native\libs\binder\Parcel.cpp
frameworks\base\core\jni\android_os_Parcel.cpp(Parcel.java中的native方法都在这个类里面)

先看addService(点击查看原图,放大看)

parcel-addService

接着分析getService(点击查看原图,放看)

parcel-getService

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值