美文网首页
Android Binder机制原理java层系列三(有图有代码

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

作者: 二全 | 来源:发表于2019-05-13 18:35 被阅读0次

该篇文章主要是根据上一篇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(点击查看原图,放大看)

addService-Parcel分析.png

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

getService-Parcel分析.png

相关文章

网友评论

      本文标题:Android Binder机制原理java层系列三(有图有代码

      本文链接:https://www.haomeiwen.com/subject/nboooqtx.html