Android13 ActivityThread main流程分析

当通过Activity startActivity,如果Activity对应的Thread不存(App未启动)在就通过Zygote创建一个,之后会运行到所在App的ActivityThread的main方法,代码如下:

//frameworks/base/core/java/android/app/ActivityThread.java
public final class ActivityThread extends ClientTransactionHandler
        implements ActivityThreadInternal {
    public static void main(String[] args) {
        Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ActivityThreadMain");


        // Install selective syscall interception
        AndroidOs.install();


        // CloseGuard defaults to true and can be quite spammy.  We
        // disable it here, but selectively enable it later (via
        // StrictMode) on debug builds, but using DropBox, not logs.
        CloseGuard.setEnabled(false);


        Environment.initForCurrentUser();


        // Make sure TrustedCertificateStore looks in the right place for CA certificates
        final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());
        TrustedCertificateStore.setDefaultUserDirectory(configDir);


        // Call per-process mainline module initialization.
        initializeMainlineModules();


        Process.setArgV0("<pre-initialized>");


        Looper.prepareMainLooper();


        // Find the value for {@link #PROC_START_SEQ_IDENT} if provided on the command line.
        // It will be in the format "seq=114"
        long startSeq = 0;
        if (args != null) {
            for (int i = args.length - 1; i >= 0; --i) {
                if (args[i] != null && args[i].startsWith(PROC_START_SEQ_IDENT)) {
                    startSeq = Long.parseLong(
                            args[i].substring(PROC_START_SEQ_IDENT.length()));
                }
            }
        }
        ActivityThread thread = new ActivityThread();
        thread.attach(false, startSeq);


        if (sMainThreadHandler == null) {
            sMainThreadHandler = thread.getHandler();
        }


        if (false) {
            Looper.myLooper().setMessageLogging(new
                    LogPrinter(Log.DEBUG, "ActivityThread"));
        }


        // End of event ActivityThreadMain.
        Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
        Looper.loop();


        throw new RuntimeException("Main thread loop unexpectedly exited");
    }
}

上面方法主要处理如下:

1、通过new的方式创建ActivityThread对象。

2、调用ActivityThread的attach方法。

下面分别进行分析:

new ActivityThread

通过new的方式创建ActivityThread对象,ActivityThread的构造方法:

//frameworks/base/core/java/android/app/ActivityThread.java
public final class ActivityThread extends ClientTransactionHandler implements ActivityThreadInternal {
    final ApplicationThread mAppThread = new ApplicationThread(); //创建了ApplicationThread
    private final ResourcesManager mResourcesManager;
    ActivityThread() {
        mResourcesManager = ResourcesManager.getInstance();
    }
}

ActivityThread attach

调用ActivityThread的attach方法:

Android13 ActivityThread attach流程分析-CSDN博客

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值