App启动原理

2021-06-21  本文已影响0人  Owen270
1.当我们点击app桌面图标的时候,桌面Launcher应用会调用

startActivity---->
startActivityForActivityResult----->
Insrumention(execStartActivity)--->
ActivityManagerProxy(startActivity)--->
Binder通信---->通知ActivityManagerService.

2.ActivityManagerService在收到AMP启动Activity的请求后,会做以下事情:

A:检查Activity的合法性,是否在AndroidManifest.xml清单文件中注册。
B:如果合法,会暂存Activity的信息,通过ActivityStack获取栈顶的Activity,并通知Launcher应用,pause 该Activity 。

C:检查Activity所在的进程是否存在。

2.1.如果Activity所在的进程不存在

A:会调用Process.start() ,通过Socket通信的方式,通知zygote进程,fork一个新进程.
B:载入ActivityThread ,会调用main()方法初始化,在main()方法内部会调用 thread.attach(false, startSeq)--->actachApplication()
B1:判断Application是否存在,如果存在,直接绑定当前的进程,
如果不存在,会调用LoadApk.makeApplication创建一个新的Application,并绑定当前进程。

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

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

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

2.2.然后AMS服务端这边,会通过ApplicationThreadProxy(ATP)ApplicationThead在服务端的代理类,调用ScheduleLanuchActivity启动Activity,以AIDL通信的方式,通知ApplicationThread启动Activity。
2.3.ApplicationThread ------->通过Handler发送一条LaunchActivity的消息通知ActivityThread,然后ActivityThread会调用

handleLaunchActivity--->performLaunchActivity---->Instrumentation--->newActivity---->callActivityCreate---->callActivityStart--->callActivityResume.----->UI渲染完成。

image.png
上一篇 下一篇

猜你喜欢

热点阅读