美文网首页Android知识Android开发Android技术知识
从ActivityManagerNative看Android系统

从ActivityManagerNative看Android系统

作者: genylife | 来源:发表于2016-10-26 17:01 被阅读1520次

    作者Blog


    看这个之前,可以先去看看大神对于Binder机制的分析,在这里附上地址

    了解Binder机制


    看完之后,再来看IActivityManager,ActivityManagerNative,ActivityManagerService这三者之间的关系。

    IActivityManager:这就相当于AIDL中定义的接口,其中包括三个部分

    public interface IActivityManager extends IInterface{
    // 省略一大波代码
    }
    
    1. 对该接口的完整类名描述
    String descriptor = "android.app.IActivityManager";
    
    1. 接口方法定义
    public int startActivity(/*参数忽略*/) throws RemoteException;
    // 此处省略N条方法定义
    
    1. 接口方法的transaction code(后面有用到的)定义
    int START_ACTIVITY_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+2;
    // 此处省略N条transaction code定义
    

    ActivityManagerNative:接口的Binder机制实现类,NOTICE:并没有实现IActivityManager中定义的方法,这个后面再说;这里面分为两部分

    1. ActivityManagerNative (对应AIDL生成的Stub类)
    public abstract class ActivityManagerNative extends Binder implements IActivityManager{
        // 1 . 构造函数
        public ActivityManagerNative() {    
            attachInterface(this, descriptor);
        }
        // 2 . asInterface函数,转换Binder对象到Interface,有些情况会生成代理对象
        static public IActivityManager asInterface(IBinder obj) {
            if (obj == null) {    
                return null;
            }
            IActivityManager in = (IActivityManager)obj.queryLocalInterface(descriptor);
            if (in != null) {
            return in;
    }
    return new ActivityManagerProxy(obj);
    }
        // 3 . onTransact函数,通过code(这个code就是之前在IActivityManager中定义的transaction code)
    //     找到对应的方法,进行调用
        public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
            throws RemoteException {
              // 省略一小撮代码
      }
    // 4 . asBinder函数,返回Binder对象,就是自己
    public IBinder asBinder() {
        return this;
    }
    // 省略一大波代码
    }
    
    1. ActivityManagerProxy(对应Stub的内部类Proxy)
    class ActivityManagerProxy implements IActivityManager{
    //  远程Binder对象
    private IBinder mRemote;
    // 1. 构造函数
    public ActivityManagerProxy(IBinder remote){
        mRemote = remote;
    }
    // 2. asBinder函数,返回远程Binder对象
    public IBinder asBinder(){
        return mRemote;
    }
    // 3. 代理对象的接口的实现
    public int startActivity(IApplicationThread caller, String callingPackage, Intent intent,
            String resolvedType, IBinder resultTo, String resultWho, int requestCode,
            int startFlags, ProfilerInfo profilerInfo, Bundle options) throws RemoteException{
                     //  代码省略,不多,可以去看看源码
    }
    // 其他方法实现省略,大同小异
    
    代理对象接口的实现基本都差不多,分为这三部分
    1. 拿到data
    
    Parcel data = Parcel.obtain();
    Parcel reply = Parcel.obtain();
    data.write...(...);
    
    2. 调用方法
    
    mRemote.transact(START_ACTIVITY_TRANSACTION, data, reply, 0);
    
    3. 返回结果
    
    reply.readException();
    int result = reply.readInt();
    reply.recycle();
    data.recycle();
    return result;
    

    ActivityManagerService:这个才是接口定义方法的真正实现类

    public final class ActivityManagerService extends ActivityManagerNative 
           implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
    public final int startActivity(IApplicationThread caller, String callingPackage,
         Intent intent, String resolvedType, IBinder resultTo,  String resultWho,
         int requestCode, int startFlags, ProfilerInfo profilerInfo, Bundle options)
           throws RemoteException{
         //  代码省略......
    }
    }
    

    接下来就看你了,可以自己去用Android Studio写一个AIDL看看生成的类,整个的结构基本上是差不多的

    作者Blog

    相关文章

      网友评论

        本文标题:从ActivityManagerNative看Android系统

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