码迷,mamicode.com
首页 > 移动开发 > 详细

android之Framework问题总结:

时间:2019-12-23 20:45:26      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:移动开发   illegal   default   runtime   主线程   ace   eth   工作   format   

 

 

1.1 ActivityThread工作原理?

=======

1.1 ActivityThread工作原理?

 

通过前面的学习(复习)我们知道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("<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");
    }

以上为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之Framework问题总结:

标签:移动开发   illegal   default   runtime   主线程   ace   eth   工作   format   

原文地址:https://www.cnblogs.com/awkflf11/p/12088145.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!