SystemServer創(chuàng)建:
流程:
->ZygoteInit.java: startSystemServer
->Zygote.java:forkSystemServer
->通過JNI調(diào)用native函數(shù),nativeForkSystemServer
上面的步驟就起來了SystemServer進程改览,后面跟 zygote進程分道揚鑣下翎;
SystemServer創(chuàng)建成功后,通過handleSystemServerProcess來進行處理
ZygoteInit.java
if (pid == 0) {
zygoteServer.closeServerSocket();
handleSystemServerProcess(parsedArgs); //通過handleSystemServer來處理
}
private static void handleSystemServerProcess(ZygoteConnection.Arguments parsedArgs){
//調(diào)用到ZygoteInit的 zygoteInit.
ZygoteInit.zygoteInit(parsedArgs.targetSdkVersion, parsedArgs.remainingArgs, cl);
}
public static final void zygoteInit(int targetSdkVersion, String[] argv,
ClassLoader classLoader) throws Zygote.MethodAndArgsCaller {
RuntimeInit.commonInit(); // (1) 做一些常規(guī)的初始化,這里不做細化
ZygoteInit.nativeZygoteInit(); // (2) 調(diào)用native函數(shù)
RuntimeInit.applicationInit(targetSdkVersion, argv, classLoader);//(3)在這里會調(diào)用到sytemserver的main函數(shù)入口
}
這邊著重看下(1)nativeZygoteInit函數(shù)宝当,這個函數(shù)實現(xiàn)在 AndroidRuntime.cpp中:
static void com_android_internal_os_ZygoteInit_nativeZygoteInit(JNIEnv* env, jobject clazz)
{
gCurRuntime->onZygoteInit();
}
gCurRuntime的定義:static AndroidRuntime* gCurRuntime = NULL;
onZygoteInit實現(xiàn)在 AndroidRuntime的子類AppRuntime中视事, Appruntime定義在app_main.c中
virtual void onZygoteInit()
{
sp<ProcessState> proc = ProcessState::self();
ALOGV("App process: starting thread pool.\n");
proc->startThreadPool(); //啟動線程,用于 Binder通信
}
接下來看下(2) Runtimeinit.applicationInit這個函數(shù)
protected static void applicationInit(int targetSdkVersion, String[] argv, ClassLoader classLoader)
throws Zygote.MethodAndArgsCaller {
//這邊執(zhí)行到systemServer的main函數(shù)
invokeStaticMain(args.startClass, args.startArgs, classLoader);
}
看下invokeStaticMain函數(shù)~
private static void invokeStaticMain(String className, String[] argv, ClassLoader classLoader)
throws Zygote.MethodAndArgsCaller {
//這里居然拋出一個異常庆揩,全局搜索下俐东,看那邊catch這個異常
throw new Zygote.MethodAndArgsCaller(m, argv);
}
全局搜索,發(fā)現(xiàn)在ZygoteInit->main里面catch了“Zygote.MethodAndArgsCaller”:
public static void main(String argv[]) {
catch (Zygote.MethodAndArgsCaller caller) {
caller.run();//居然又回調(diào)到Zygote.MethodAndArgsCaller的run,醉了
}
}
上述為什么要用異常捕獲订晌,而不是直接調(diào)用虏辫?
繼續(xù)跟蹤:
Zygote.MethodAndArgsCaller
MethodAndArgsCaller是Zygote類的內(nèi)部類:
public static class MethodAndArgsCaller extends Exception{
public void run() {
try {
mMethod.invoke(null, new Object[] { mArgs }); //反射調(diào)用,取到 SystemServer.main
}
}
}
截止到上面锈拨, 還只是創(chuàng)建SystemServer,真正開始SystemServer是從調(diào)用 main函數(shù)開始:
-> 調(diào)用SystemServer的main函數(shù)
public static void main(String[] args) {
new SystemServer().run();
}
->SystemServer().run()
這里主要是啟動一些服務~
private void run() {
//加載 android_servers庫
System.loadLibrary("android_servers");
//創(chuàng)建上下文關系
createSystemContext();
// Create the system service manager. ->創(chuàng)建service manager
mSystemServiceManager = new SystemServiceManager(mSystemContext);
mSystemServiceManager.setRuntimeRestarted(mRuntimeRestart);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
//開啟services
startBootstrapServices();
startCoreServices();
startOtherServices();
}
依次來看下開啟的幾個服務
startBootstrapServices
private void startBootstrapServices() {
//啟動ActivityManagerService
mActivityManagerService = mSystemServiceManager.startService(
ActivityManagerService.Lifecycle.class).getService();
//啟動PowerManagerService
mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);
//啟動PackageMangerService
mPackageManagerService = PackageManagerService.main(mSystemContext, installer,
mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore);
//啟動SensorService
startSensorService();
}
startCoreServices();
private void startCoreServices() {
// Tracks the battery level. Requires LightService.
mSystemServiceManager.startService(BatteryService.class);
// Tracks application usage stats.
mSystemServiceManager.startService(UsageStatsService.class);
mActivityManagerService.setUsageStatsManager(
LocalServices.getService(UsageStatsManagerInternal.class));
// Tracks whether the updatable WebView is in a ready state and watches for update installs.
mWebViewUpdateService = mSystemServiceManager.startService(WebViewUpdateService.class);
}
startOtherServices->省略掉砌庄,反正就是開啟services