美文网首页
Android Framework-ActivityRecord

Android Framework-ActivityRecord

作者: 行走中的3卡 | 来源:发表于2023-08-23 09:00 被阅读0次

    1. 作用:

    记录一个Activity的历史任务(封装了包含启动Activity过程中的信息)

    2. 路径:

    frameworks/base/services/core/java/com/android/server/wm/ActivityRecord.java

    3. 主要属性

    /**
     * An entry in the history task, representing an activity.
     */
    final class ActivityRecord extends WindowToken implements WindowManagerService.AppFreezeListener {
        private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityRecord" : TAG_ATM;// ActivityTaskManager
        
        final ActivityTaskManagerService mAtmService;
        @NonNull
        final ActivityInfo info; // activity info provided by developer in AndroidManifest
        // Which user is this running for?
        final int mUserId;
        // The package implementing intent's component
        // TODO: rename to mPackageName
        final String packageName;
        // the intent component, or target of an alias.
        final ComponentName mActivityComponent; 
        
        final int launchedFromPid; // always the pid who started the activity. //是谁启动这个Activity的
        final int launchedFromUid; // always the uid who started the activity.  //是谁启动这个Activity的
        final String launchedFromPackage; // always the package who started the activity.
    
        final Intent intent;    // the original intent that generated us // 创建这个Activity的原始Intent
        final String shortComponentName; // the short component name of the intent
        final String resolvedType; // as per original caller; // 前一个原始的调用者
        final String processName; // process where this component wants to run  
        
        ActivityRecord resultTo; // who started this entry, so will get our reply //当前Activity?
        final String resultWho; // additional identifier for use by resultTo.   
        
        ...
    

    3. toString 打印的信息

        public String toString() {
            if (stringName != null) {
                return stringName + " t" + (task == null ? INVALID_TASK_ID : task.mTaskId) +
                        (finishing ? " f}" : "") + (mIsExiting ? " isExiting" : "") + "}";
            }
            StringBuilder sb = new StringBuilder(128);
            sb.append("ActivityRecord{");
            sb.append(Integer.toHexString(System.identityHashCode(this)));
            sb.append(" u");
            sb.append(mUserId);
            sb.append(' ');
            sb.append(intent.getComponent().flattenToShortString());
            sb.append("}");
            stringName = sb.toString();
            return stringName;
        }
    

    例如: sourceRecord=ActivityRecord{e39ee2f u0 android/com.android.internal.app.ChooserActivity} t12}

    4.其它常用方法

    静态方法 isInAnyTask,根据IBinder 获取 ActivityRecord对象

        @Nullable
        static ActivityRecord isInAnyTask(IBinder token) {
            final ActivityRecord r = ActivityRecord.forTokenLocked(token);
            return (r != null && r.isAttached()) ? r : null;
        }
    

    再调用forTokenLocked

        static @Nullable ActivityRecord forTokenLocked(IBinder token) {
            final ActivityRecord r = forToken(token);
            return r == null || r.getRootTask() == null ? null : r;
        }   
    

    再再调用 forToken, 有可能不存在的

        /** Gets the corresponding record by the token. Note that it may not exist in the hierarchy. */
        @Nullable
        static ActivityRecord forToken(IBinder token) {
            if (token == null) return null;
            final Token activityToken;
            try {
                activityToken = (Token) token;
            } catch (ClassCastException e) {
                Slog.w(TAG, "Bad activity token: " + token, e);
                return null;
            }
            return activityToken.mActivityRef.get();
        }   
    

    此外,判断是否为 ResolverActivity 或者 它的子类的方法(isAssignableFrom)

        static boolean isResolverActivity(String className) {
            return ResolverActivity.class.getName().equals(className);
        }
    
        boolean isResolverOrChildActivity() {
            if (!"android".equals(packageName)) {
                return false;
            }
            try {
                return ResolverActivity.class.isAssignableFrom(
                        Object.class.getClassLoader().loadClass(mActivityComponent.getClassName()));
            } catch (ClassNotFoundException e) {
                return false;
            }
        }
    

    相关文章

      网友评论

          本文标题:Android Framework-ActivityRecord

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