相關(guān)文章鏈接:
1. Android Framework - 學(xué)習(xí)啟動篇
2. Android Framework - 開機啟動 SystemServer 進程
相關(guān)源碼文件:
/frameworks/base/services/java/com/android/server/SystemServer.java
/frameworks/base/services/core/java/com/android/server/SystemServiceManager.java
/frameworks/base/services/core/java/com/android/server/ServiceThread.java
/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
ActivityManagerService 是 Android 中非常重要的一個服務(wù)箱残,主要功能是管理和啟動四大組件邦鲫。其源代碼大約有 2W 多行哮内,這里我們主要來分析其啟動過程百宇,后面的文章我們肯定還會分析具體的源碼細(xì)節(jié)。AMS 是由 SystemServer 進程啟動的衙吩,對于這個有不了解的同學(xué)請看這里《Android Framework - 開機啟動 SystemServer 進程》
private void startBootstrapServices() {
...
// 啟動 AMS 服務(wù)
mActivityManagerService = mSystemServiceManager.startService(
ActivityManagerService.Lifecycle.class).getService();
// 設(shè)置 AMS 的系統(tǒng)服務(wù)管理器
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
// 設(shè)置 AMS 的 APP 安裝器
mActivityManagerService.setInstaller(installer);
...
// 設(shè)置 SystemServer
mActivityManagerService.setSystemProcess();
}
public static final class Lifecycle extends SystemService {
private final ActivityManagerService mService;
public Lifecycle(Context context) {
super(context);
// 創(chuàng)建 ActivityManagerService
mService = new ActivityManagerService(context);
}
@Override
public void onStart() {
// 調(diào)用 start 方法
mService.start();
}
// 獲取 ActivityManagerService
public ActivityManagerService getService() {
return mService;
}
}
// Note: This method is invoked on the main thread but may need to attach various
// handlers to other threads. So take care to be explicit about the looper.
public ActivityManagerService(Context systemContext) {
mContext = systemContext;
// 獲取 ActivityThread
mSystemThread = ActivityThread.currentActivityThread();
// 創(chuàng)建名為"ActivityManager"的前臺 HandlerThread
mHandlerThread = new ServiceThread(TAG,
android.os.Process.THREAD_PRIORITY_FOREGROUND, false /*allowIo*/);
// 啟動 ServiceThread
mHandlerThread.start();
// 創(chuàng)建一個 MainHandler 與 mHandlerThread 公用一個 looper
mHandler = new MainHandler(mHandlerThread.getLooper());
// 創(chuàng)建 UiHandler 其內(nèi)部也會創(chuàng)建一個 HandlerThread
mUiHandler = new UiHandler();
// 前臺和后臺廣播接收隊列望抽,分別是 10s 和 60s 放棄執(zhí)行
mFgBroadcastQueue = new BroadcastQueue(this, mHandler,
"foreground", BROADCAST_FG_TIMEOUT, false);
mBgBroadcastQueue = new BroadcastQueue(this, mHandler,
"background", BROADCAST_BG_TIMEOUT, true);
mBroadcastQueues[0] = mFgBroadcastQueue;
mBroadcastQueues[1] = mBgBroadcastQueue;
// 創(chuàng)建目錄/data/system
File dataDir = Environment.getDataDirectory();
File systemDir = new File(dataDir, "system");
systemDir.mkdirs();
// 創(chuàng)建 BatteryStatsService,相當(dāng)于電表
mBatteryStatsService = new BatteryStatsService(systemDir, mHandler);
mBatteryStatsService.getActiveStatistics().readLocked();
mBatteryStatsService.scheduleWriteToDisk();
mBatteryStatsService.getActiveStatistics().setCallback(this);
// 創(chuàng)建進程統(tǒng)計服務(wù)塞耕,信息保存在目錄 /data/system/procstats,
mProcessStats = new ProcessStatsService(this, new File(systemDir, "procstats"));
...
// 創(chuàng)建 ActivityStackSupervisor 對象
mStackSupervisor = new ActivityStackSupervisor(this, mRecentTasks);
mTaskPersister = new TaskPersister(systemDir, mStackSupervisor, mRecentTasks);
// 創(chuàng)建名為 "CpuTracker" 的線程
mProcessCpuThread = new Thread("CpuTracker") {
@Override
public void run() {
while (true) {
try {
...
updateCpuStatsNow();
} catch (Exception e) {
Slog.e(TAG, "Unexpected exception collecting process stats", e);
}
}
}
};
// 看門狗
Watchdog.getInstance().addMonitor(this);
Watchdog.getInstance().addThread(mHandler);
}
在 SystemService 進程的啟動過程中會調(diào)用 startBootstrapServices 方法郊艘,其內(nèi)部會啟動 AMS 服務(wù)荷科,在 ActivityManagerService 的構(gòu)造函數(shù)中會創(chuàng)建三個線程分別是 ActivityManager 、UI 和 CpuTracker纱注。我們接著看 start 方法:
private void start() {
// 移除所有的進程組
Process.removeAllProcessGroups();
// 啟動 CpuTracker 線程
mProcessCpuThread.start();
// 把電表服務(wù)注冊添加到 ServiceManager
mBatteryStatsService.publish(mContext);
mAppOpsService.publish(mContext);
Slog.d("AppOps", "AppOpsService published");
LocalServices.addService(ActivityManagerInternal.class, new LocalService());
}
public void setSystemProcess() {
try {
// 把自己加到 ServiceManager ,然后注冊額外的一些服務(wù)
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));
// 通過 pms 獲取 ApplicationInfo 信息
ApplicationInfo info = mContext.getPackageManager().getApplicationInfo(
"android", STOCK_PM_FLAGS);
// 初始化 ApplicationInfo 和 ClassLoader
mSystemThread.installSystemApplicationInfo(info, getClass().getClassLoader());
synchronized (this) {
// 創(chuàng)建 ProcessRecord 對象
ProcessRecord app = newProcessRecordLocked(info, info.processName, false, 0);
app.persistent = true;
app.pid = MY_PID;
app.maxAdj = ProcessList.SYSTEM_ADJ;
app.makeActive(mSystemThread.getApplicationThread(), mProcessStats);
synchronized (mPidsSelfLocked) {
mPidsSelfLocked.put(app.pid, app);
}
updateLruProcessLocked(app, false, null);
updateOomAdjLocked();
}
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(
"Unable to find android system package", e);
}
}
private void startOtherServices() {
...
// 安裝系統(tǒng) Provider
mActivityManagerService.installSystemProviders();
...
mActivityManagerService.systemReady(new Runnable() {
public void run() {
// 啟動 WebView
WebViewFactory.prepareWebViewInSystemServer();
// 啟動 SystemUi
startSystemUi(context);
// 調(diào)用一系列服務(wù)的 systemReady 方法
...
// 調(diào)用一系列服務(wù)的 systemRunning 方法
...
}
}
}
public void systemReady(final Runnable goingCallback) {
...
// 執(zhí)行 Callback 的 run 方法
if (goingCallback != null) goingCallback.run();
// Start up initial activity.
mBooting = true;
// 啟動桌面 Activity 進程
startHomeActivityLocked(mCurrentUserId, "systemReady");
}
static final void startSystemUi(Context context) {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.android.systemui",
"com.android.systemui.SystemUIService"));
//Slog.d(TAG, "Starting service: " + intent);
context.startServiceAsUser(intent, UserHandle.OWNER);
}
關(guān)于 AMS 的啟動流程還是很簡單的胆胰,調(diào)用構(gòu)造函數(shù)會初始化三個線程狞贱;調(diào)用 setSystemProcess 方法中會向 ServcieManager 進程額外發(fā)布一些服務(wù):procstats(進程信息)、meminfo(內(nèi)存信息)蜀涨、gfxinfo(圖形信息)瞎嬉、cpuinfo(cpu信息)、permission(權(quán)限)厚柳、processinfo(應(yīng)用使用情況)等氧枣;調(diào)用 systemReady 方法首先會啟動 SystemUIService,然后執(zhí)行一系列服務(wù)的 systemReady 和 systemRunning 方法别垮,最后啟動桌面 Activity 進程便监。
視頻地址:https://pan.baidu.com/s/1dM3K9bPRuepFUJIASIDeQg
視頻密碼:af97