SystemServer被zygote啓動後會運行到main函數,在這裏面直接運行到run方法
public static void main(String[] args) {
new SystemServer().run();
}
在run方法中,會做一些初始化,之後啓動其他服務,最終進入Loop循環
private void run() {
Looper.prepareMainLooper();
nativeInit();
createSystemContext();
mSystemServiceManager = new SystemServiceManager(mSystemContext);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
startBootstrapServices();
startCoreServices();
startOtherServices();
Looper.loop();
}
nativeInit函數在com_android_server_SystemServer.cpp中,開啓傳感器服務
SensorService::instantiate();
createSystemContext函數創建ActivityThread,並創建mSystemContext
private void createSystemContext() {
ActivityThread activityThread = ActivityThread.systemMain();
mSystemContext = activityThread.getSystemContext();
mSystemContext.setTheme(android.R.style.Theme_DeviceDefault_Light_DarkActionBar);
}
在startBootstrapServices中通過classloader加載類名並調用他們的start方法開啓了一些重要的的系統服務
mActivityManagerService
mPowerManagerService
mDisplayManagerService
mPackageManagerService
在startCoreService中開啓了
LightsService
BatteryService
UsageStatsService
UsageStatsManagerInternal
WebViewUpdateService
在startOtherServices中進行了一大堆初始化,最後運行到初始化UI
// 這裏啓動了launcher
mActivityManagerService.systemReady()
startSystemUi(context)
這裏是發送了一個Intent
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);
}
這裏實際是運行到了SystemUIApplication,就是啓動一些系統UI
private final Class<?>[] SERVICES = new Class[] {
com.android.systemui.keyguard.KeyguardViewMediator.class,
com.android.systemui.recent.Recents.class,
com.android.systemui.volume.VolumeUI.class,
com.android.systemui.statusbar.SystemBars.class,
com.android.systemui.usb.StorageNotification.class,
com.android.systemui.power.PowerUI.class,
com.android.systemui.media.RingtonePlayer.class
};