在SystemServer中簡單分析了AMS的流程,這篇就詳細地講解AMS的啓動流程java
注意一點:AMS所在的進程是屬於SystemServer中的android
在startBootstrapServices以前有一個流程是 createSystemContext,該過程會建立對象有ActivityThread,Instrumentation, ContextImpl,LoadedApk,Application,這些都是後面AMS要用到的對象,請注意它們是在這裏建立的app
private void createSystemContext() {
ActivityThread activityThread = ActivityThread.systemMain();
mSystemContext = activityThread.getSystemContext();
mSystemContext.setTheme(DEFAULT_SYSTEM_THEME);
final Context systemUiContext = activityThread.getSystemUiContext();
systemUiContext.setTheme(DEFAULT_SYSTEM_THEME);
}
複製代碼
private void startBootstrapServices() {
...
//啓動AMS服務
mActivityManagerService = mSystemServiceManager.startService(
ActivityManagerService.Lifecycle.class).getService();
//設置AMS的系統服務管理器
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
//設置AMS的APP安裝器
mActivityManagerService.setInstaller(installer);
//初始化AMS相關的PMS
mActivityManagerService.initPowerManagement();
...
//設置SystemServer
mActivityManagerService.setSystemProcess();
}
複製代碼
public SystemService startService(String className) {
final Class<SystemService> serviceClass;
try {
serviceClass = (Class<SystemService>)Class.forName(className);
} catch (ClassNotFoundException ex) {
......
}
return startService(serviceClass);
}
public void startService(@NonNull final SystemService service) {
// Register it.
mServices.add(service);
// Start it.
long time = SystemClock.elapsedRealtime();
try {
service.onStart();
} catch (RuntimeException ex) {
.....
}
warnIfTooLong(SystemClock.elapsedRealtime() - time, service, "onStart");
}
複製代碼
這部分比較簡單,主要是添加服務,並調用服務的 onStart 函數ide
public static final class Lifecycle extends SystemService {
private final ActivityManagerService mService;
public Lifecycle(Context context) {
super(context);
mService = new ActivityManagerService(context);
}
@Override
public void onStart() {
mService.start();
}
@Override
public void onCleanupUser(int userId) {
mService.mBatteryStatsService.onCleanupUser(userId);
}
public ActivityManagerService getService() {
return mService;
}
}
複製代碼
繼承SystemService,表明是一個系統服務,裏面主要是建立 ActivityManagerService函數
public ActivityManagerService(Context systemContext) {
mContext = systemContext;
mFactoryTest = FactoryTest.getMode();
mSystemThread = ActivityThread.currentActivityThread();
//建立名爲"ActivityManager"的前臺線程,並獲取mHandler
mHandlerThread = new ServiceThread(TAG, android.os.Process.THREAD_PRIORITY_FOREGROUND, false);
mHandlerThread.start();
mHandler = new MainHandler(mHandlerThread.getLooper());
//經過UiThread類,建立名爲"android.ui"的線程
mUiHandler = new UiHandler();
......
//建立目錄/data/system
File dataDir = Environment.getDataDirectory();
File systemDir = new File(dataDir, "system");
systemDir.mkdirs();
//建立服務BatteryStatsService
mBatteryStatsService = new BatteryStatsService(systemDir, mHandler);
mBatteryStatsService.getActiveStatistics().readLocked();
......
//建立進程統計服務,信息保存在目錄/data/system/procstats,
mProcessStats = new ProcessStatsService(this, new File(systemDir, "procstats"));
......
//CPU使用狀況的追蹤器執行初始化
mProcessCpuTracker.init();
......
mRecentTasks = new RecentTasks(this);
// 建立ActivityStackSupervisor對象
mStackSupervisor = new ActivityStackSupervisor(this, mRecentTasks);
mTaskPersister = new TaskPersister(systemDir, mStackSupervisor, mRecentTasks);
//建立名爲"CpuTracker"的線程
mProcessCpuThread = new Thread("CpuTracker") {
public void run() {
......
}
};
......
}
複製代碼
該過程共建立了3個線程oop
mSystemServiceManager.startService 會調用服務的onStart,在AMS裏onStart會調用他的startui
private void start() {
Process.removeAllProcessGroups(); //移除全部的進程組
mProcessCpuThread.start(); //開啓CpuTracker線程
mBatteryStatsService.publish(mContext); //啓動電池統計服務
mAppOpsService.publish(mContext);
//建立LocalService,並添加到LocalServices
LocalServices.addService(ActivityManagerInternal.class, new LocalService());
}
複製代碼
這個步驟仍是在 startBootstrapServices 中執行的this
public void setSystemProcess() {
try {
ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true);
ServiceManager.addService(ProcessStats.SERVICE_NAME, mProcessStats);
ServiceManager.addService("meminfo", new MemBinder(this));
ServiceManager.addService("gfxinfo", new GraphicsBinder(this));
ServiceManager.addService("dbinfo", new DbBinder(this));
if (MONITOR_CPU_USAGE) {
ServiceManager.addService("cpuinfo", new CpuBinder(this));
}
ServiceManager.addService("permission", new PermissionController(this));
ServiceManager.addService("processinfo", new ProcessInfoService(this));
ApplicationInfo info = mContext.getPackageManager().getApplicationInfo(
"android", STOCK_PM_FLAGS | MATCH_SYSTEM_ONLY);
mSystemThread.installSystemApplicationInfo(info, getClass().getClassLoader());
......
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(
"Unable to find android system package", e);
}
}
複製代碼
若是以前看過筆者寫的binder系列,應該知道 ServiceManager.addService 主要就是添加服務spa
該函數的主要做用是註冊服務,以及爲系統進程設置Application單例並啓動,是由 installSystemApplicationInfo 函數完成的線程
該函數最終會執行到 LoadedApk 的 installSystemApplicationInfo 函數,來設置包名爲 'android' 的 package 信息
/** * Sets application info about the system package. */
void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
assert info.packageName.equals("android");
mApplicationInfo = info;
mClassLoader = classLoader;
}
複製代碼
在執行完startBootstrapServices後,最後會運行 startOtherServices 函數
private void startOtherServices() {
......
//安裝系統Provider
mActivityManagerService.installSystemProviders();
......
mActivityManagerService.systemReady(new Runnable() {
public void run() {
......
}
}
}
複製代碼
安裝服務,就是在某某鏈表中添加
installSystemProviders 中會調用 mSystemThread.installSystemProviders(providers)
try {
ActivityManager.getService().publishContentProviders(
getApplicationThread(), results);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
複製代碼
那最終調用到 AMS的 publishContentProviders 函數,這個函數就是把providers放到map裏等等操做
關於 systemReady 在SystemServer的啓動流程中介紹過了
AMS是最後一個啓動的服務,會調用 mActivityManagerService.systemReady
mActivityManagerService.systemReady(new Runnable() {
public void run() {
//啓動WebView
WebViewFactory.prepareWebViewInSystemServer();
//啓動系統UI
startSystemUi(context);
// 執行一系列服務的systemReady方法
networkScoreF.systemReady();
networkManagementF.systemReady();
networkStatsF.systemReady();
......
//執行一系列服務的systemRunning方法
wallpaper.systemRunning();
inputMethodManager.systemRunning(statusBarF);
location.systemRunning();
}
});
複製代碼
public final class ActivityManagerService{
public void systemReady(final Runnable goingCallback) {
...//更新操做
mSystemReady = true; //系統處於ready狀態
removeProcessLocked(proc, true, false, "system update done");//殺掉全部非persistent進程
mProcessesReady = true; //進程處於ready狀態
goingCallback.run(); //這裏有可能啓動進程
addAppLocked(info, false, null); //啓動全部的persistent進程
mBooting = true; //正在啓動中
startHomeActivityLocked(mCurrentUserId, "systemReady"); //啓動桌面
mStackSupervisor.resumeTopActivitiesLocked(); //恢復棧頂的Activity
}
}
複製代碼