【Android 启动过程】Activity 启动源码分析 ( ActivityThread 流程分析 一 )(一)

文章目录

一、ActivityThread 主函数启动

二、ActivityThread 绑定 ApplicationThread

三、AMS attachApplication -> attachApplicationLocked 绑定 ApplicationThread

四、ApplicationThread.bindApplication 绑定 ApplicationThread

五、ActivityThread.H 处理 BIND_APPLICATION 消息

六、ActivityThread.handleBindApplication 处理绑定问题

七、LoadedApk.makeApplication 创建 Application 对象

八、Instrumentation.newApplication 创建 Application 对象

九、AppComponentFactory.instantiateApplicationCompat 创建 Application 对象





一、ActivityThread 主函数启动


ActivityThread 是应用的主线程 , 从 main 函数开始执行 ;


Looper.prepareMainLooper() 将主线程设置为 Looper 线程 , 开启 Looper , 用于配合 H 处理消息 ;


thread.attach(false, startSeq) 绑定 ActivityThread ;


在方法最后 Looper.loop(); 开始无限循环 , 处理 Handler 消息 ;


/**
 * 管理应用程序进程中主线程的执行、调度和执行活动、广播以及活动管理器请求的其他操作。
 *
 * {@hide}
 */
public final class ActivityThread extends ClientTransactionHandler {
    public static void main(String[] args) {
  // 将主线程设置为 Looper 线程 , 开启 Looper , 用于配合 H 处理消息 
        Looper.prepareMainLooper();
  // 创建 ActivityThread 实例对象 
        ActivityThread thread = new ActivityThread();
        // 绑定
        thread.attach(false, startSeq);
  // 开始无限循环 , 处理 Handler 消息 
        Looper.loop();
        throw new RuntimeException("Main thread loop unexpectedly exited");
    }
}


完整代码参考 /frameworks/base/core/java/android/app/ActivityThread.java






二、ActivityThread 绑定 ApplicationThread


ActivityThread 中的 void attach(boolean system, long startSeq) 方法 , 主要是通过 Binder 机制获取 AMS , 并调用 AMS 的 attachApplication 方法 , 为 ActivityThread 绑定 ApplicationThread ;


/**
 * 管理应用程序进程中主线程的执行、调度和执行活动、广播以及活动管理器请求的其他操作。
 *
 * {@hide}
 */
public final class ActivityThread extends ClientTransactionHandler {
    private void attach(boolean system, long startSeq) {
        sCurrentActivityThread = this;
        mSystemThread = system;
        if (!system) {
            RuntimeInit.setApplicationObject(mAppThread.asBinder());
            // 通过 Binder 机制获取 AMS 
            final IActivityManager mgr = ActivityManager.getService();
            try {
                // 调用 AMS 的 attachApplication 方法 , 为 ActivityThread 绑定 ApplicationThread 
                mgr.attachApplication(mAppThread, startSeq);
            } catch (RemoteException ex) {
                throw ex.rethrowFromSystemServer();
            }
        }
    }
}


完整代码参考 /frameworks/base/core/java/android/app/ActivityThread.java






三、AMS attachApplication -> attachApplicationLocked 绑定 ApplicationThread


在 AMS 中的 attachApplication 方法中 , 调用了 attachApplicationLocked 方法 ,


在 attachApplicationLocked 方法中 , 有调用了 ActivityThread 的 bindApplication 方法 , 为 ActivityThread 绑定了 ApplicationThread ;


public class ActivityManagerService extends IActivityManager.Stub
        implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
    @Override
    public final void attachApplication(IApplicationThread thread) {
        synchronized (this) {
            int callingPid = Binder.getCallingPid();
            final long origId = Binder.clearCallingIdentity();
            attachApplicationLocked(thread, callingPid);
            Binder.restoreCallingIdentity(origId);
        }
    }
    // 为 ApplicationThread 绑定 Application 主方法
    private final boolean attachApplicationLocked(IApplicationThread thread,
            int pid) {
        try {
            checkTime(startTime, "attachApplicationLocked: immediately before bindApplication");
            mStackSupervisor.mActivityMetricsLogger.notifyBindApplication(app);
    // 在此处为 ActivityThread 绑定 Application , 此时又回到 ActivityThread
            if (app.instr != null) {
                thread.bindApplication(processName, appInfo, providers,
                        app.instr.mClass,
                        profilerInfo, app.instr.mArguments,
                        app.instr.mWatcher,
                        app.instr.mUiAutomationConnection, testMode,
                        mBinderTransactionTrackingEnabled, enableTrackAllocation,
                        isRestrictedBackupMode || !normalMode, app.persistent,
                        new Configuration(getGlobalConfiguration()), app.compat,
                        getCommonServicesLocked(app.isolated),
                        mCoreSettingsObserver.getCoreSettingsLocked(),
                        buildSerial);
            } else {
                thread.bindApplication(processName, appInfo, providers, null, profilerInfo,
                        null, null, null, testMode,
                        mBinderTransactionTrackingEnabled, enableTrackAllocation,
                        isRestrictedBackupMode || !normalMode, app.persistent,
                        new Configuration(getGlobalConfiguration()), app.compat,
                        getCommonServicesLocked(app.isolated),
                        mCoreSettingsObserver.getCoreSettingsLocked(),
                        buildSerial);
            }
        } catch (Exception e) {
        }
        return true;
    }
}


ActivityManagerService 完整源码参考 : frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java






四、ApplicationThread.bindApplication 绑定 ApplicationThread


再次回到 ActivityThread 内部类 ApplicationThread 中 , 调用 ApplicationThread 类的 bindApplication 方法 , 即可为 ActivityThread 绑定 ApplicationThread , 在所有数据就位后 , 发送了一个 H.BIND_APPLICATION 消息 ;


/**
 * 管理应用程序进程中主线程的执行、调度和执行活动、广播以及活动管理器请求的其他操作。
 *
 * {@hide}
 */
public final class ActivityThread extends ClientTransactionHandler {
    private class ApplicationThread extends IApplicationThread.Stub {
  // 为 ActivityThread 绑定 Application 
        public final void bindApplication(String processName, ApplicationInfo appInfo,
                List<ProviderInfo> providers, ComponentName instrumentationName,
                ProfilerInfo profilerInfo, Bundle instrumentationArgs,
                IInstrumentationWatcher instrumentationWatcher,
                IUiAutomationConnection instrumentationUiConnection, int debugMode,
                boolean enableBinderTracking, boolean trackAllocation,
                boolean isRestrictedBackupMode, boolean persistent, Configuration config,
                CompatibilityInfo compatInfo, Map services, Bundle coreSettings,
                String buildSerial, boolean autofillCompatibilityEnabled) {
            // 此处在所有数据就位后 , 发送了一个 H.BIND_APPLICATION 消息
            sendMessage(H.BIND_APPLICATION, data);
        }
    }
}


完整代码参考 /frameworks/base/core/java/android/app/ActivityThread.java





上一篇:ASP.NET Core MVC请求超时设置解决方案


下一篇:Swift3.0:PhotoKit的使用