Android ActivityThread工作原理

导读

  • 移动开发知识体系总章(Java基础、Android、Flutter)
  • Android Handler消息机制 、 Android中为什么主线程不会因为Looper.loop里的无限循环ANR?

通过前面的学习(复习)我们知道ActivityThread其实不是一个Thread,而是一个final类型的Java类,并且拥有main(String[] args) 方法。Android原生以Java语言为基础,Java的JVM启动的入口就是main(String[] args)。

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

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

以上为main方法中的全部代码,我们关注以下几点:

  • Looper.prepareMainLooper();
  • Looper.loop();
  • thread.attach(false, startSeq);
  • thread.getHandler();

Looper.prepareMainLooper();

主程序Looper的初始化工作

    public static void prepareMainLooper() {
        prepare(false);
        synchronized (Looper.class) {
            if (sMainLooper != null) {
                throw new IllegalStateException("The main Looper has already been prepared.");
            }
            sMainLooper = myLooper();
        }
    }
    private static void prepare(boolean quitAllowed) {
        if (sThreadLocal.get() != null) {
            throw new RuntimeException("Only one Looper may be created per thread");
        }
        sThreadLocal.set(new Looper(quitAllowed));
    }
    public static @Nullable Looper myLooper() {
        return sThreadLocal.get();
    }

可以看到new Looper的时候传了false

Looper.loop();

1.Looper就是字面意思(轮训器),在loop方法中无限循环的去MessageQueue(消息队列)中读取消息。

  1. MessageQueue则通过next方法无限循环的进行消息出队,无消息时则会进入睡眠

执行thread.attach(false, startSeq);

通过thread.attach(false, startSeq);把ActivityThread 和主线程进行绑定。

执行thread.getHandler();

你可能感兴趣的:(Android ActivityThread工作原理)