SystemServer进程的创建及Launcher启动
1.SystemServer进程的创建
ZygoteInit.main方法中里fork出个SystemServer进程
1 | /* Hardcoded command line to start the system server */ |
2.SystemServer.main方法
1 | public static void main(String[] args) { |
run方法
2.1. 创建loop对象
1 | // Prepare the main looper thread (this thread). |
2.2 创建了系统上下文
1 | // Initialize the system context. |
创建AcitivityThread
这里创建的ActivityThread 和ActivityThread.main()中创建的不是同一个1
2
3
4
5
6
7
8
9
10private void createSystemContext() {
// systemMain()中创建activityThread 调用activityThread .attach(true, 0); 会创建Application,Instrumentation ,并调用applicition.onCreate()
ActivityThread activityThread = ActivityThread.systemMain();
//创建context,创建LoadedApk
mSystemContext = activityThread.getSystemContext();
mSystemContext.setTheme(DEFAULT_SYSTEM_THEME);
final Context systemUiContext = activityThread.getSystemUiContext();
systemUiContext.setTheme(DEFAULT_SYSTEM_THEME);
}
2.3启动SystemServiceManager
1 | // Create the system service manager. |
2.4 启动相关服务
1 | // Start services. |
2.4.1startBootstrapServices()启动了ActivityManagerService ,PowerManagerService,PackageManagerService …等
2.4.2
startCoreServices()启动了BatteryService,UsageStatsService,WebViewUpdateService
2.4.3 startOtherServices()启动了AlarmManagerService,BluetoothService,NotificationManagerService …等
mActivityManagerService.systemReady 启动launcher1
2
3
4
5
6// We now tell the activity manager it is okay to run third party
// code. It will call back into us once it has gotten to the state
// where third party code can really run (but before it has actually
// started launching the initial applications), for us to complete our
// initialization.
mActivityManagerService.systemReady(...)
systemReady 方法中调用 mActivityManagerService.startHomeActivityLocked启动launcher1
2
startHomeActivityLocked(currentUserId, "systemReady");
下面代码1处调用ActivityStarter中的 startActivityMayWait 或者startActivity ,又开始走Activity启动流程1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
mSupervisor.moveHomeStackTaskToTop(reason);
//代码1
mLastHomeActivityStartResult = obtainStarter(intent, "startHomeActivity: " + reason)
.setOutActivity(tmpOutRecord)
.setCallingUid(0)
.setActivityInfo(aInfo)
.execute();
mLastHomeActivityStartRecord = tmpOutRecord[0];
if (mSupervisor.inResumeTopActivity) {
// If we are in resume section already, home activity will be initialized, but not
// resumed (to avoid recursive resume) and will stay that way until something pokes it
// again. We need to schedule another resume.
mSupervisor.scheduleResumeTopActivities();
}
}
2.5进入消息循环
1 | // Loop forever. |