一、AMS概述
????????AMS(ActivityManagerService)是Android中最核心的服务,主要负责系统中四大组件的启动、切换、调度及应用进程的管理和调度等工作,其职责与操作系统中的进程管理和调度模块相类似。 ?AMS服务运行在system_server进程中,AMS由SystemServer的ServerThread线程创建。
二、AMS
????????ActivityManagerService的启动是在systemserver进程的startBootstrapServices方法中启动的. ActivityManagerService.Lifecycle.startService(mSystemServiceManager, atm);?
///android/frameworks/base/services/java/com/android/server/SystemServer.java
public static void main(String[] args) {
new SystemServer().run();
}
private void run() {
... // 创建Looper
Looper.prepareMainLooper();
// 1、加载动态库android_servers(C,C++ 文件编译产物)
System.loadLibrary("android_servers");
// Check whether we failed to shut down last time we tried.
// This call may not return.
performPendingShutdown();
// Initialize the system context.
createSystemContext();
// 2、创建system service manager。对系统的服务进行创建、启动、
//生命周期管理
mSystemServiceManager = new SystemServiceManager(mSystemContext);
mSystemServiceManager.setRuntimeRestarted(mRuntimeRestart);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
// Prepare the thread pool for init tasks that can be parallelized
SystemServerInitThreadPool.get();
...
//3、分批启动系统服务
//启动引导服务
startBootstrapServices();
//启动核心服务
startCoreServices();
//启动其他服务
startOtherServices();
...
// Loop forever.
Looper.loop();
throw new RuntimeException("Main thread loop unexpectedly exited");
}
......
private void startBootstrapServices(@NonNull TimingsTraceAndSlog t) {
t.traceBegin("startBootstrapServices");
....
//Installer跟安装apk相关的东西
Installer installer = mSystemServiceManager.startService(Installer.class);
t.traceEnd();
.........
//ATM
ActivityTaskManagerService atm = mSystemServiceManager.startService(
ActivityTaskManagerService.Lifecycle.class).getService();
//AMS初始化过程,Lifecycle说明AMS也是具有生命周期的。
mActivityManagerService = ActivityManagerService.Lifecycle.startService(
mSystemServiceManager, atm);
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
mActivityManagerService.setInstaller(installer);
mWindowManagerGlobalLock = atm.getGlobalLock();
t.traceEnd();
//传感器相关的
mSystemServiceManager.startService(SensorNotificationService.class);
}
AMS Lifecycle:
//android/frameworks/base/services/core/java/com/android/server/wm/ActivityManagerService.java
public class ActivityManagerService extends IActivityTaskManager.Stub {
...
public static final class Lifecycle extends SystemService {
private final ActivityManagerService mService;
private static ActivityTaskManagerService sAtm;
public Lifecycle(Context context) {
super(context);
mService = new ActivityManagerService(context,sAtm);
}
......
//利用getService()提供给AMS
public ActivityManagerService getService() {
return mService;
}
}
AMS构造方法:
//android/frameworks/base/services/core/java/com/android/server/wm/ActivityManagerService.java
public ActivityManagerService(Context systemContext, ActivityTaskManagerService atm) {
//handler的处理
mHandlerThread = new ServiceThread(TAG,
THREAD_PRIORITY_FOREGROUND, false /*allowIo*/);
mHandlerThread.start();
mHandler = new MainHandler(mHandlerThread.getLooper());
mUiHandler = mInjector.getUiHandler(this);
....
//电量服务相关的
mBatteryStatsService = new BatteryStatsService(systemContext, systemDir,
BackgroundThread.get().getHandler());
mBatteryStatsService.getActiveStatistics().readLocked();
mBatteryStatsService.scheduleWriteToDisk();
...
//统计应用程序状态相关的
mProcessStats = new ProcessStatsService(this, new File(systemDir, "procstats"));
//调用ATMS中的initialize()
mActivityTaskManager = atm;
mActivityTaskManager.initialize(mIntentFirewall, mPendingIntentController,
DisplayThread.get().getLooper());
像activity栈管理对象的创建在安卓10.0之后都放在了ATMS中了。看看ATMS中的initialize()
public void initialize(IntentFirewall intentFirewall, PendingIntentController intentController,
947 Looper looper) {
948 mH = new H(looper);
949 mUiHandler = new UiHandler();
950 mIntentFirewall = intentFirewall;
//系统目录的创建
951 final File systemDir = SystemServiceManager.ensureSystemDir();
952 mAppWarnings = createAppWarnings(mUiContext, mH, mUiHandler, systemDir);
953 mCompatModePackages = new CompatModePackages(this, systemDir, mH);
954 mPendingIntentController = intentController;
//创建activity栈
955 mTaskSupervisor = createTaskSupervisor();
956 mActivityClientController = new ActivityClientController(this);
957
958 mTaskChangeNotificationController =
959 new TaskChangeNotificationController(mGlobalLock, mTaskSupervisor, mH);
960 mLockTaskController = new LockTaskController(mContext, mTaskSupervisor, mH,
961 mTaskChangeNotificationController);
962 mActivityStartController = new ActivityStartController(this);
//最近任务栈
963 setRecentTasks(new RecentTasks(this, mTaskSupervisor));
//VR相关的初始化
964 mVrController = new VrController(mGlobalLock);
965 mKeyguardController = mTaskSupervisor.getKeyguardController();
966 mPackageConfigPersister = new PackageConfigPersister(mTaskSupervisor.mPersisterQueue);
967 }
AMS:我们用的adb命令就是连接到了下面这些服务。
/android/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
public void setSystemProcess() {
1858 try {
1859 ServiceManager.addService(Context.ACTIVITY_SERVICE, this, /* allowIsolated= */ true,
1860 DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PRIORITY_NORMAL | DUMP_FLAG_PROTO);
1861 ServiceManager.addService(ProcessStats.SERVICE_NAME, mProcessStats);
1862
//adb shell dumpsys meminf 这个名字对接就是这些服务meminfo。
ServiceManager.addService("meminfo", new MemBinder(this), /* allowIsolated= */ false,
1863 DUMP_FLAG_PRIORITY_HIGH);
1864 ServiceManager.addService("gfxinfo", new GraphicsBinder(this));
//数据库信息
1865 ServiceManager.addService("dbinfo", new DbBinder(this));
1866 mAppProfiler.setCpuInfoService();
//权限信息
1867 ServiceManager.addService("permission", new PermissionController(this));
//进程信息
1868 ServiceManager.addService("processinfo", new ProcessInfoService(this));
1869 ServiceManager.addService("cacheinfo", new CacheBinder(this));
1870
}
SystemServer:
//其他服务
private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
1363 t.traceBegin("startOtherServices");
1364
1365 final Context context = mSystemContext;
1366 DynamicSystemService dynamicSystem = null;
1367 IStorageManager storageManager = null;
1368 NetworkManagementService networkManagement = null;
1369 IpSecService ipSecService = null;
1370 VpnManagerService vpnManager = null;
1371 VcnManagementService vcnManagement = null;
1372 NetworkStatsService networkStats = null;
1373 NetworkPolicyManagerService networkPolicy = null;
1374 NsdService serviceDiscovery = null;
1375 WindowManagerService wm = null;
1376 SerialService serial = null;
1377 NetworkTimeUpdateService networkTimeUpdater = null;
1378 InputManagerService inputManager = null;
1379 TelephonyRegistry telephonyRegistry = null;
1380 ConsumerIrService consumerIr = null;
1381 MmsServiceBroker mmsService = null;
1382 HardwarePropertiesManagerService hardwarePropertiesService = null;
1383 PacProxyService pacProxyService = null;
......
//安装系统的内容提供者,去系统设置里面调字体大小,就可以对下面这个api做文章
t.traceBegin("InstallSystemProviders");
1469 mActivityManagerService.getContentProviderHelper().installSystemProviders();
1470 // Now that SettingsProvider is ready, reactivate SQLiteCompatibilityWalFlags
1471 SQLiteCompatibilityWalFlags.reset();
1472 t.traceEnd();
//WMS
t.traceBegin("StartWindowManagerService");
1525 // WMS needs sensor service ready
1526 mSystemServiceManager.startBootPhase(t, SystemService.PHASE_WAIT_FOR_SENSOR_SERVICE);
1527 wm = WindowManagerService.main(context, inputManager, !mFirstBoot, mOnlyCore,
1528 new PhoneWindowManager(), mActivityManagerService.mActivityTaskManager);
1529 ServiceManager.addService(Context.WINDOW_SERVICE, wm, /* allowIsolated= */ false,
1530 DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PROTO);
1531 ServiceManager.addService(Context.INPUT_SERVICE, inputManager,
1532 /* allowIsolated= */ false, DUMP_FLAG_PRIORITY_CRITICAL);
1533 t.traceEnd();
//到这个方法基本的服务都启动完成,接着就是Lunch启动
mActivityManagerService.systemReady(() -> {
}
}
AMS:
public void systemReady(final Runnable goingCallback, TimingsTraceLog traceLog) {
if (bootingSystemUser) {
mAtmInternal.startHomeOnAllDisplays(currentUserId, "systemReady");
}
}
在创建完AMS之后,system_server的run方法会执行到startOtherServices(),在启动“其他服务”完毕之后,会调入到AMS的systemReady()方法,在这里会启动launcher。 可以说,在这个方法执行完毕之后,系统就已经启动完成了。如果我们先要在系统启动的过程中在java framework中加入自己的代码,可以在systemReady()这个方法中,完成。(比如注册自己的广播接受器) ?
|