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
};