ATM和AMS启动流程

AMS 即 ActivityManagerService,负责 Activy、Service、Broadcast、ContentProvider 四大组件的生命周期管理。本文主要介绍 AMS 的启动流程和初始化过程。AMS 在初始化的过程中,也伴随着了ATMS(ActivityTaskManagerService)的初始化

记得我们在system_server进程启动流程这一篇讲过,里面其中包括了创建SystemServerManager,再看下源码,在SystemServer类中run方法中:

startBootstrapServices:

 跳转到startBootstrapServices中去看:

注:10.0之后添加了ActivityTaskManagerService这个服务 ,简称ATM,这里启动了ATM服务

接下来看ActivityTaskManagerService Lifecyle这个类

 public static final class Lifecycle extends SystemService {
1006          private final ActivityTaskManagerService mService;
1007  
1008          public Lifecycle(Context context) {
1009              super(context);
1010              mService = new ActivityTaskManagerService(context);
1011          }
1012  
1013          @Override
1014          public void onStart() {
1015              publishBinderService(Context.ACTIVITY_TASK_SERVICE, mService);
1016              mService.start();
1017          }
1018  
1019          @Override
1020          public void onUnlockUser(int userId) {
1021              synchronized (mService.getGlobalLock()) {
1022                  mService.mStackSupervisor.onUserUnlocked(userId);
1023              }
1024          }
1025  
1026          @Override
1027          public void onCleanupUser(int userId) {
1028              synchronized (mService.getGlobalLock()) {
1029                  mService.mStackSupervisor.mLaunchParamsPersister.onCleanupUser(userId);
1030              }
1031          }
1032  
1033          public ActivityTaskManagerService getService() {
1034              return mService;
1035          }
1036      }
1037  

构造函数中创建了ATM这个服务,

     @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
723      public ActivityTaskManagerService(Context context) {
724          mContext = context;
725          mFactoryTest = FactoryTest.getMode();
726          mSystemThread = ActivityThread.currentActivityThread();
727          mUiContext = mSystemThread.getSystemUiContext();
728          mLifecycleManager = new ClientLifecycleManager();
729          mInternal = new LocalService();
730          GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version", GL_ES_VERSION_UNDEFINED);
731          mWindowOrganizerController = new WindowOrganizerController(this);
732          mTaskOrganizerController = mWindowOrganizerController.mTaskOrganizerController;
733      }

在 ATM构造函数中,有其中这样一段代码:

mLifecycleManager = new ClientLifecleManager();创建了Activity生命周期管理类。

回过头来看ActivityTaskManagerService Lifecycle类中的onStart方法里面publishBinderService,这个方法做了什么呢

看SystemService中的这个方法:

最终调用的是ServiceManager addService,也就是将ATM这个服务添加到ServiceManager中。

ATM创建完成,接着就是AMS创建

调用的是AMS中Lifecycle中的startService

 public static final class Lifecycle extends SystemService {
2324          private final ActivityManagerService mService;
2325          private static ActivityTaskManagerService sAtm;
2326  
2327          public Lifecycle(Context context) {
2328              super(context);
2329              mService = new ActivityManagerService(context, sAtm);
2330          }
2331  
2332          public static ActivityManagerService startService(
2333                  SystemServiceManager ssm, ActivityTaskManagerService atm) {
2334              sAtm = atm;
2335              return ssm.startService(ActivityManagerService.Lifecycle.class).getService();
2336          }
2337  
2338          @Override
2339          public void onStart() {
2340              mService.start();
2341          }
2342  
2343          @Override
2344          public void onBootPhase(int phase) {
2345              mService.mBootPhase = phase;
2346              if (phase == PHASE_SYSTEM_SERVICES_READY) {
2347                  mService.mBatteryStatsService.systemServicesReady();
2348                  mService.mServices.systemServicesReady();
2349              } else if (phase == PHASE_ACTIVITY_MANAGER_READY) {
2350                  mService.startBroadcastObservers();
2351              } else if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
2352                  mService.mPackageWatchdog.onPackagesReady();
2353              }
2354          }
2355  
2356          @Override
2357          public void onCleanupUser(int userId) {
2358              mService.mBatteryStatsService.onCleanupUser(userId);
2359          }
2360  
2361          public ActivityManagerService getService() {
2362              return mService;
2363          }
2364      }

再调用的是ServiceManager里面的startService。Lifecycle构造函数中初始化了AMS,和在ATM中的Lifecycle类似,创建了ATM服务。

看ATM的这个构造函数

 

代码很多,我这边很多就截个图,具体的小伙伴们自己可以去源码中去看。

到ActivityTaskManager中的initialize方法里面去看下哈,

 public void initialize(IntentFirewall intentFirewall, PendingIntentController intentController,
837              Looper looper) {
838          mH = new H(looper);
839          mUiHandler = new UiHandler();
840          mIntentFirewall = intentFirewall;
841          final File systemDir = SystemServiceManager.ensureSystemDir();
842          mAppWarnings = createAppWarnings(mUiContext, mH, mUiHandler, systemDir);
843          mCompatModePackages = new CompatModePackages(this, systemDir, mH);
844          mPendingIntentController = intentController;
845          mStackSupervisor = createStackSupervisor();
846  
847          mTaskChangeNotificationController =
848                  new TaskChangeNotificationController(mGlobalLock, mStackSupervisor, mH);
849          mLockTaskController = new LockTaskController(mContext, mStackSupervisor, mH);
850          mActivityStartController = new ActivityStartController(this);
851          setRecentTasks(new RecentTasks(this, mStackSupervisor));
852          mVrController = new VrController(mGlobalLock);
853          mKeyguardController = mStackSupervisor.getKeyguardController();
854      }

其中mStackSupervisor = createStackSupervisor() 创建了Activity栈管理对象。

setRecentTask:设置当前任务栈

AMS和ATM是相关联的,那么代码中哪一块设置了它们之间的关系呢?

我们再次回到SystemServer中去,看代码,如下图红色框线中所示

SystemServer中startBootstrapService 后接着有比较重要的一些方法,比如setSystemProcess

  public void setSystemProcess() {
2100          try {
2101              ServiceManager.addService(Context.ACTIVITY_SERVICE, this, /* allowIsolated= */ true,
2102                      DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PRIORITY_NORMAL | DUMP_FLAG_PROTO);
2103              ServiceManager.addService(ProcessStats.SERVICE_NAME, mProcessStats);
2104              ServiceManager.addService("meminfo", new MemBinder(this), /* allowIsolated= */ false,
2105                      DUMP_FLAG_PRIORITY_HIGH);
2106              ServiceManager.addService("gfxinfo", new GraphicsBinder(this));
2107              ServiceManager.addService("dbinfo", new DbBinder(this));
2108              if (MONITOR_CPU_USAGE) {
2109                  ServiceManager.addService("cpuinfo", new CpuBinder(this),
2110                          /* allowIsolated= */ false, DUMP_FLAG_PRIORITY_CRITICAL);
2111              }
2112              ServiceManager.addService("permission", new PermissionController(this));
2113              ServiceManager.addService("processinfo", new ProcessInfoService(this));
2114              ServiceManager.addService("cacheinfo", new CacheBinder(this));
2115  
2116              ApplicationInfo info = mContext.getPackageManager().getApplicationInfo(
2117                      "android", STOCK_PM_FLAGS | MATCH_SYSTEM_ONLY);
2118              mSystemThread.installSystemApplicationInfo(info, getClass().getClassLoader());
2119  
2120              synchronized (this) {
2121                  ProcessRecord app = mProcessList.newProcessRecordLocked(info, info.processName,
2122                          false,
2123                          0,
2124                          new HostingRecord("system"));
2125                  app.setPersistent(true);
2126                  app.pid = MY_PID;
2127                  app.getWindowProcessController().setPid(MY_PID);
2128                  app.maxAdj = ProcessList.SYSTEM_ADJ;
2129                  app.makeActive(mSystemThread.getApplicationThread(), mProcessStats);
2130                  addPidLocked(app);
2131                  mProcessList.updateLruProcessLocked(app, false, null);
2132                  updateOomAdjLocked(OomAdjuster.OOM_ADJ_REASON_NONE);
2133              }
2134          } catch (PackageManager.NameNotFoundException e) {
2135              throw new RuntimeException(
2136                      "Unable to find android system package", e);
2137          }
2138  
2139          // Start watching app ops after we and the package manager are up and running.
2140          mAppOpsService.startWatchingMode(AppOpsManager.OP_RUN_IN_BACKGROUND, null,
2141                  new IAppOpsCallback.Stub() {
2142                      @Override public void opChanged(int op, int uid, String packageName) {
2143                          if (op == AppOpsManager.OP_RUN_IN_BACKGROUND && packageName != null) {
2144                              if (getAppOpsManager().checkOpNoThrow(op, uid, packageName)
2145                                      != AppOpsManager.MODE_ALLOWED) {
2146                                  runInBackgroundDisabled(uid);
2147                              }
2148                          }
2149                      }
2150                  });
2151  
2152          final int[] cameraOp = {AppOpsManager.OP_CAMERA};
2153          mAppOpsService.startWatchingActive(cameraOp, new IAppOpsActiveCallback.Stub() {
2154              @Override
2155              public void opActiveChanged(int op, int uid, String packageName, boolean active) {
2156                  cameraActiveChanged(uid, active);
2157              }
2158          });
2159      }

主要做的就是添加一些service。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值