ActivityThread

ActivityThread并不是一个线程,是一个普通Java类。它有一个main函数,由于在Java程序中main函数就是程序的入口函数,因此这里的main函数就是整个app的真正入口,也就是APP启动后会首先运行的方法。看一下main方法,

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

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

这里创建并开启了主线程的Looper循环,看一下这个Looper的创建,


屏幕截图 2021-07-26 175309.png

可以看到,首先,prepare传参quitAllowed为false,继而把它传给MassageQueue,代表此Looper不可以退出,它不同于普通线程的Looper传的是true,可以退出。 因为Android是事件驱动,整个APP的正常运行建立在主线程的这个无限循环上,正是主线程不停地经由Looper从MassageQueue中取消息处理,才得以实现APP所有功能,比如收到数据页面刷新,点击,滑动等。所以假如MainLooper退出,那么APP进程也就终止了。
接下来在attach方法中,ActivityManagerService通过attachApplication方法,将ApplicationThread对象绑定到ActivityManagerService,ApplicationThread是ActivityThread的私有内部类,实现了IBinder接口,用于和其他进程通信,比如在启动activity过程中,ApplicationThread接受来自系统进程中ClientLifeCycleManager的事务。

sMainThreadHandler是Handler的一个子类,在其handleMessage方法中把接收到的各种类型的消息都分别调用相应的处理方法,比如退出APP,接收广播消息,开启服务等。
屏幕截图 2021-07-26 182302.png

在attach方法中,还会调用ActivityManagerService的attachApplication方法,放两张网上大佬的源码图,


屏幕截图 2021-07-26 191445.png
屏幕截图 2021-07-26 191510.png

可以看到,在attachApplication方法中,调用了attachApplicationLocked方法,然后在此方法中调用bindApplication来创建Application类。然后在attachApplicationLocked方法中,该方法最终会调用ApplicationThread类的scheduleLaunchActivity方法,发送创建activity实例的消息到ActivityThread,由其完成activity的创建。

——以上就是个人浅显的理解了,同时感谢网上大佬的分享。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容