Activity启动第一章

启动位置:frameworks/base/core/java/android/app/ActivityThread.java

    public static void main(String[] args) {
        Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ActivityThreadMain");
        SamplingProfilerIntegration.start();

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

        // Set the reporter for event logging in libcore
        EventLogger.setReporter(new EventLoggingReporter());

        // 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();
        //关键点:从这里开始
        ActivityThread thread = new ActivityThread();
        thread.attach(false);

        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方法中创建了ActivityThread对象,该对象调用attach(false)方法
 ActivityThread thread = new ActivityThread();
thread.attach(false);

 private void attach(boolean system) {
        //去掉了其他业务代码
       final IActivityManager mgr = ActivityManager.getService();
       try {
            mgr.attachApplication(mAppThread);
        } catch (RemoteException ex) {
            throw ex.rethrowFromSystemServer();
        }
        //去掉了其他业务代码
    }

在这里面,mAppThread 是ApplicationThread一个对象,ApplicationThread是ActivityThread的一个内部类

ApplicationThread extends IApplicationThread.Stub  可以看出,它采用Android体系中最重要的代理模式,并且实现了所有和Acitivity交互相关的接口,这里我们就可以猜想它是用来在ActivityThread和ActivityManagerService进程间通信的

 

在IActivityManager中的attachApplication方法中把ApplicationThread 和 ActivityManagerService关联了起来,为下一步ActivityManagerService 控制该Acitivity做了准备

 

 

 

 

点赞