android api 26 ActivityManagerNative类被弃用。代理类ActivityManagerProxy已经被删除。改用AIDL方式。

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qi1017269990/article/details/78879512

今天撸源码:startActivity()。

发现了在API 26中: ActivityManagerNative类被弃用,代理类ActivityManagerProxy已经被删除。

本篇文章主要是记下,API26和API25的不同。

参考了文章:Activity启动过程详解

看到Instrumentation类的exeStartActivity时候,源码如下:

 public ActivityResult execStartActivity(
            Context who, IBinder contextThread, IBinder token, Activity target,
            Intent intent, int requestCode, Bundle options) {
        IApplicationThread whoThread = (IApplicationThread) contextThread;
        Uri referrer = target != null ? target.onProvideReferrer() : null;
        if (referrer != null) {
            intent.putExtra(Intent.EXTRA_REFERRER, referrer);
        }
        if (mActivityMonitors != null) {
            synchronized (mSync) {
                final int N = mActivityMonitors.size();
                for (int i=0; i<N; i++) {
                    final ActivityMonitor am = mActivityMonitors.get(i);
                    ActivityResult result = null;
                    if (am.ignoreMatchingSpecificIntents()) {
                        result = am.onStartActivity(intent);
                    }
                    if (result != null) {
                        am.mHits++;
                        return result;
                    } else if (am.match(who, null, intent)) {
                        am.mHits++;
                        if (am.isBlocking()) {
                            return requestCode >= 0 ? am.getResult() : null;
                        }
                        break;
                    }
                }
            }
        }
        try {
            intent.migrateExtraStreamToClipData();
            intent.prepareToLeaveProcess(who);
            int result = ActivityManager.getService()//注意这一行,和参考文章不同!
                .startActivity(whoThread, who.getBasePackageName(), intent,
                        intent.resolveTypeIfNeeded(who.getContentResolver()),
                        token, target != null ? target.mEmbeddedID : null,
                        requestCode, 0, null, options);
            checkStartActivityResult(result, intent);
        } catch (RemoteException e) {
            throw new RuntimeException("Failure from system", e);
        }
        return null;
    }
发现参考文章使用的如图:


怎么两句话不同呢?其实原来我使用的api是26,而26之前的代码使用的是原作者写的


继续深入看两个不同的代码:

26:

 /**
     * @hide
     */
    public static IActivityManager getService() {
        return IActivityManagerSingleton.get();
    }

    private static final Singleton<IActivityManager> IActivityManagerSingleton =
            new Singleton<IActivityManager>() {
                @Override
                protected IActivityManager create() {
                    final IBinder b = ServiceManager.getService(Context.ACTIVITY_SERVICE);
                    final IActivityManager am = IActivityManager.Stub.asInterface(b);//注意这一行
                    return am;
                }
            };

25:

 static public IActivityManager getDefault() {
        return gDefault.get();
    }

 private static final Singleton<IActivityManager> gDefault = new Singleton<IActivityManager>() {
        protected IActivityManager create() {
            IBinder b = ServiceManager.getService("activity");
            if (false) {
                Log.v("ActivityManager", "default service binder = " + b);
            }
            IActivityManager am = asInterface(b);//注意这一行
            if (false) {
                Log.v("ActivityManager", "default service = " + am);
            }
            return am;
        }
    };

发现两个代码都是返回的IActivityManager。不同的是:

25中使用的是:静态方法asInterface,该方法属于ActivityManagerNative类。源码:   

 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);
    }

参考文章中说明了ActivityManagerNative类,还有ActivityManagerProxy类,是如何实现和AMS进程间通信的。最终使用ActivityManagerProxy代理类实现了startActivity。具体见参考文章:Activity启动过程详解

26中使用的是:

IActivityManager.Stub.asInterface(b);

而我们再看api26中的ActivityManagerNative类。源码已经很少:

/**
 * {@hide}
 * @deprecated will be removed soon. See individual methods for alternatives.//注意解释
 */
@Deprecated
public abstract class ActivityManagerNative {
    /**
     * Cast a Binder object into an activity manager interface, generating
     * a proxy if needed.
     *
     * @deprecated use IActivityManager.Stub.asInterface instead.//注意解释
     */
    static public IActivityManager asInterface(IBinder obj) {
        return IActivityManager.Stub.asInterface(obj);
    }

    /**
     * Retrieve the system's default/global activity manager.
     *
     * @deprecated use ActivityManager.getService instead.
     */
    static public IActivityManager getDefault() {
        return ActivityManager.getService();
    }

    /**
     * Convenience for checking whether the system is ready.  For internal use only.
     *
     * @deprecated use ActivityManagerInternal.isSystemReady instead.
     */
    static public boolean isSystemReady() {
        return ActivityManager.isSystemReady();
    }

    /**
     * @deprecated use ActivityManager.broadcastStickyIntent instead.
     */
    static public void broadcastStickyIntent(Intent intent, String permission, int userId) {
        broadcastStickyIntent(intent, permission, AppOpsManager.OP_NONE, userId);
    }

    /**
     * Convenience for sending a sticky broadcast.  For internal use only.
     * If you don't care about permission, use null.
     *
     * @deprecated use ActivityManager.broadcastStickyIntent instead.
     */
    static public void broadcastStickyIntent(Intent intent, String permission, int appOp,
            int userId) {
        ActivityManager.broadcastStickyIntent(intent, appOp, userId);
    }

    /**
     * @deprecated use ActivityManager.noteWakeupAlarm instead.
     */
    static public void noteWakeupAlarm(PendingIntent ps, int sourceUid, String sourcePkg,
            String tag) {
        ActivityManager.noteWakeupAlarm(ps, sourceUid, sourcePkg, tag);
    }

    /**
     * @deprecated use ActivityManager.noteAlarmStart instead.
     */
    static public void noteAlarmStart(PendingIntent ps, int sourceUid, String tag) {
        ActivityManager.noteAlarmStart(ps, sourceUid, tag);
    }

    /**
     * @deprecated use ActivityManager.noteAlarmFinish instead.
     */
    static public void noteAlarmFinish(PendingIntent ps, int sourceUid, String tag) {
        ActivityManager.noteAlarmFinish(ps, sourceUid, tag);
    }
}

解释说这个类已经被弃用,方法都可以用ActivityManager来中的代替。

而且代理类ActivityManagerProxy已经被删除!



小结:API26中启动Activity时候,和AMS通信机制已经被改成AIDL方式!


扩展思考:

1、其实通过看aidl 自动生成文件,可以看出,

stub.asInterface其实调用的也是queryLocalInterface,api25和api26的本质是一样的。


2、api26中只有ServiceManagerNative还没有被弃用。

25中使用的:

ApplicationThreadNative、
ActivityManagerNative、
BulkCursorNative、
ContentProviderNative、

都已经不在使用。

展开阅读全文

没有更多推荐了,返回首页