Android 相关启动执行流程

2022-09-02  本文已影响0人  丹丹无敌

Android相关流程图

init:

  1. 挂载和创建系统文件
  2. 解析rc文件
  3. 进入无限循环
    1. 执行action -- zygote启动
    2. 检测并重启进程
    3. 接收子进程的SIGCHLD信号,执行响应的方法

zygote:

  1. native
    1. 初始化Android运行环境
    2. 注册jni
    3. 执行ZygoteInit.main 方法 进入Java层
  2. Java
    1. 创建 socket
    2. 执行预加载
    3. 通过fork创建 SystemServer 进程
    4. 进入循环:等待AMS的通知,创建对应的进程

SystemServer
nice-name 给进程的名称

pid = Zygote.forkSystemServer

//启动 SystemServer.main
return handleSystemServerProcess(parsedArgs);
    //开启Binder线程池
    ZygoteInit.nativeZygoteInit();
        gCurRuntime->onZygoteInit();
             sp<ProcessState> proc = ProcessState::self();
                proc->startThreadPool();
    // 运行 SystemServer.main
    RuntimeInit.applicationInit
        // 通过反射执行 SystemServer.main
        mMethod.invoke(null, new Object[] { mArgs }); 

SystemServer进程:

  1. createSystemContext();
    //执行systemMain()@ActivityThread,在systemMain方法中

    1. new ActivityThread(),执行attach()@ActivityThread
      //在attach方法中
    2. new Instrumentation(),
    3. ContextImpl context = ContextImpl.createAppContext(
      this, getSystemContext().mPackageInfo);
    4. Application mInitialApplication = context.mPackageInfo.makeApplication(true, null);
  2. mSystemServiceManager = new SystemServiceManager(mSystemContext); // 系统服务

    1. startService --> service.onStart(); // 继承自 SystemService 的服务
  3. startBootstrapServices(t); // 引导服务 --AMS
    //android 10开始 多了个 ATMS(ActivityTaskManagerService)

    1. mService = new ActivityTaskManagerService(context);

      mService.start();

       ServiceManager.addService(name, service,resumeTopActivityInnerLocked allowIsolated, dumpPriority);
      
    2. 创建 AMS -- 其他三个服务,持有 ATMS
      mService = new ActivityManagerService(context, sAtm);

      mService.start();

  4. startCoreServices(t); // 核心服务

  5. startOtherServices(t); // 其他服务 -- WMS

    mActivityManagerService.systemReady // 启动Launcher

    startSystemUi(context, windowManagerF); // 启动 SystemUI

mActivityManagerService.systemReady -->

mActivityTaskManager.onSystemReady@ActivityTaskManagerService -->

mAtmInternal.resumeTopActivities@ActivityTaskManagerService -->

mRootWindowContainer.resumeFocusedStacksTopActivities()@RootWindowContainer -->

resumeFocusedStacksTopActivities@RootWindowContainer -->

targetStack.resumeTopActivityUncheckedLocked@ActivityStack

--> resumeTopActivityInnerLocked@ActivityStack -->

resumeNextFocusableActivityWhenStackIsEmpty@ActivityStack

--> mRootWindowContainer.resumeHomeActivity@RootWindowContainer -->

startHomeOnTaskDisplayArea@RootWindowContainer -->

resolveHomeActivity@RootWindowContainer -->

mService.getActivityStartController().startHomeActivity@ActivityStartController obtainStarter@ActivityStartController -->

execute@ActivityStarter -->

executeRequest@ActivityStarter -->

startActivityInner@ActivityStarter

这里调用了startActivity()方法来启动Launcher,最终会进入Launcher的onCreate()方法,Launcher启动完成。

mSystemServiceManager.startBootPhase // 状态设置

SystemServiceManager:管理服务的生命周期

ServerManager:管理Binder服务的

ServiceManager.getService(Context.ACTIVITY_TASK_SERVICE);

publishBinderService(Context.ACTIVITY_TASK_SERVICE, mService);

ServiceManager.addService(name, service, allowIsolated, dumpPriority); // 注册Binder服务

Activity启动流程:

startActivity@Activity --> startActivityForResult@Activity

--> mInstrumentation.execStartActivity@Instrumentation -->

ActivityTaskManager.getService().startActivity@ActivityTaskManagerService

--> startActivityAsUser@ActivityTaskManagerService

--> execute@ActivityStarter -->

executeRequest@ActivityStarter -->

startActivityUnchecked@ActivityStarter -->

startActivityInner@ActivityStarter -->

resumeFocusedStacksTopActivities@RootWindowContainer -->

resumeTopActivityUncheckedLocked@ActivityStack -->

resumeTopActivityInnerLocked@ActivityStack -->

startSpecificActivity@ActivityStackSupervisor -->

realStartActivityLocked@ActivityStackSupervisor -->

scheduleTransaction@ClientLifecycleManager -->

schedule@ClientTransaction -->

scheduleTransaction@ApplicationThread -->

scheduleTransaction@Activity -->

execute@TransactionExecutor -->

executeCallbacks@TransactionExecutor -->

execute@LaunchActivityItem -->

handleLaunchActivity@ActivityThread -->

performLaunchActivity@ActivityThread -->

createBaseContextForActivity@ActivityThread 、mInstrumentation.newActivity、makeApplication@LoadApk -->

attach@Activity、callActivityOnCreate@Instrumentation -->

onCreate@Activity

小结

  1. 调用从system_server进程传过来的ClientTransaction.CallBack类中的execute方法,其继续调用ActivityThread.handleLaunchActivity方法来启动Activity。
  2. ActivityThread.handleLaunchActivity调用performLaunchActivity方法启动Activity
  3. performLaunchActivity先获取启动Activity的ActivityInfo,而后创建一个LoadedApk和上下文ContextImpl。在通过反射去创建此Activity对象,并执行Activity的onCreate方法。

SystemServer执行流程图:


image.png

Activity启动:


image.png

Android 启动流程时序图:


image.png

Activity生命周期管理类图:


image.png

下图是ATMS处理startActivity过程,并回调启动进程的ApplicationThread:


22650779-eabf22046e2f33d4.png

下图为Android 30 之后,ActivityThread启动Activity过程的时序图:


22650779-72d2d3d9acda317d.png
上一篇下一篇

猜你喜欢

热点阅读