源码笔记-应用启动流程

源码笔记-应用启动流程

标签(空格分隔): android 源码 framwork


1、ActivityManagerService.startProcessLocked()会执行ActivityThread.main()方法,去启动应用。

    private final void startProcessLocked(ProcessRecord app String hostingType, String hostingName, String abiOverride, String entryPoint, String[] entryPointargs) {
        ...//忽略
        if (entryPoint == null) {
            entryPoint = "android.app.ActivityThread";
        }
        Process.start(entryPoint, ...);
    }

2、在ActivityThread.main()中,会先执行以下几步操作:

1、Looper.prepareMainLooper()方法,将当前线程跟Looper绑定;
2、创建一个新的ActivityThread实例;
3、执行ActivityThread.attach()创建application以及Activity
4、执行Looper.loop()方法,遍历MessageQueue

ActivityThread.java
public static void main(String[] args) {
    ...
    Looper.prepareMainLooper(); //当前线程跟Looper绑定
    ActivityThread thread = new ActivityThread();
    thread.attach(false);       //false表示非系统应用
    Looper.loop();
}

private void attach(boolean system) {
    if (!system) {
        ...
        final IActiivtyManager mgr = ActivityManagerNative.getDefault(); //返回ActivityManagerService
        try {
            mgr.attachApplication(mAppThread);
        }
        ...
    }
    ...
}


3、在ActivityManagerService.attachApplication()中,会执行attachApplicationLocked()方法。

ActivityManagerService.java
private final attachApplicationLockded(IApplicationThread thread, int pid) {
    ...
    thread.bindApplication();   //创建Application
    mStackSupervisor.attachApplicationLocked(app);  //创建并启动Launcher Activity
}

代码中的threadActivityThread.ApplicationThread的实例,所以接下去查看bindApplication()的内容;

4、ActivityThread.ApplicationThread.bindApplication()会通过Handler发送一个BIND_APPLICATIONmessage,并通过ActivityThread.handleBindApplication()方法处理

private void handleBindApplication() {
    ...
    Application app = data.info.makeApplication();  //创建Application,并执行attach();
    mInstrumentation.callApplicationOnCreate(app);//执行Application的onCreate()
}

5、LoadedApk.makeApplication()方法会创建返回application,并且执行application.attach()方法

LoadedApk.java
public Application makeApplication(boolean forceDefaultAppClass, Instrumentation instrumentation) {
    ...
    Application app = null;
    String appClass = mApplicationInfo.className;
    if (forceDefaultAppClass || (appClass == null)) {
        appClass = "android.app.Application";
    }
    ...
    app = mActivityThread.mInstrumentation.newApplication(cl, appClass, appContext);
    ...
    return app;
}

Instrumentation.java
static public Application newApplication(Class clazz, Context context) {
    Application app = (Application)clazz.newInstance();
    app.attach(context);
    return app;
}

从上面的代码中看到如果使用默认的APPClass或者appClassnull的时候,会使用android.app.Applicaiton类,并通过反射去实例化Application,同时会执行app.attach()方法,所以当我们的manifest中,Application标签的name会默认为android.app.Applicaiton.
6、看完ActivityThread.bindApplication()后,我们来看ActivityStackSupervisor.attachApplicationLocked();

ActivityStackSuppervisor.java
boolean attachApplicationLocked(ProcessRecord app) {
    ...
    try {
        if (realStartActivityLocked(hr, app, true, true)) {
            didSomething = true;
        }
    } cach (RemoteException e) {
        
    }
    ...
}

final boolean realStartActivityLocked() {
    ...
    app.thread.scheduleLaunchActivity();
    ...
}

可以看出最终会调用到IApplicationThreadscheduleLaunchActivity()。接下来转过去看看该方法的实现。

ActivityThread.java

public final void scheduleLaunchActivity() {
    ...
    sendMessage(H.LAUNCH_ACTIVITY, r);  //发送launch Activity的消息
}

private void handleLaunchActivity() {
    ...
    WindowManagerGlobal.initialize();
    Activity a = performLaunchActivity(r, customIntent);
    if (a != null) {
        handleResumeActivity();
    }
    ...
}

private Activity performLaunchActivity() {
    ...
    Activity activity = null;
    try {
        java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
        activity = mInstrumentation.newActivity();
    }
    ...
    activity.attach();  //attach
    mInstrumentation.callActivityOnCreate(activity, r.state);//onCreate
    activity.performStart();    //执行onStart
    ...
}

通过代码可以看到,performLaunchActivity()会通过反射创建Activity的实例,并先后执行ActivityattachonCreateonStart()等生命周期方法。handleResumeActivity()会执行ActivityonResume()生命周期,到这一步,可以说我们的app已经启动起来了。从创建进程,到创建application再到创建launch Activity

你可能感兴趣的:(源码笔记-应用启动流程)