frameworks/base/core/java/com/android/internal/os/ZygoteInit.java文件中:

Step1、startSystemServer函数

[java] view plain copy
  1. publicclassZygoteInit{
  2. ......
  3. privatestaticbooleanstartSystemServer()
  4. throwsMethodAndArgsCaller,RuntimeException{
  5. /*Hardcodedcommandlinetostartthesystemserver*/
  6. Stringargs[]={
  7. "--setuid=1000",
  8. "--setgid=1000",
  9. "--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,3001,3002,3003",
  10. "--capabilities=130104352,130104352",
  11. "--runtime-init",
  12. "--nice-name=system_server",
  13. "com.android.server.SystemServer",
  14. };
  15. ZygoteConnection.ArgumentsparsedArgs=null;
  16. intpid;
  17. try{
  18. parsedArgs=newZygoteConnection.Arguments(args);
  19. ......
  20. /*Requesttoforkthesystemserverprocess*/
  21. //startSystemServer()调用Zygote的native方法 forkSystemServer(),java端的Zygote的准备工作就结束了,接下来就交给C/C++端的Zygote来执行fork任务了,在/dalvik/vm/native/dalvik_system_Zygote.c 中,Dalvik_dalvik_system_Zygote_forkSystemServer函数中(自己去看源码)
  22. 这个里面的fork进程主要是使用linux的fork进程
  23. //Zygote进程通过Zygote.forkSystemServer函数来创建一个新的进程来启动SystemServer组件
  24. pid=Zygote.forkSystemServer(
  25. parsedArgs.uid,parsedArgs.gid,
  26. parsedArgs.gids,debugFlags,null,
  27. parsedArgs.permittedCapabilities,
  28. parsedArgs.effectiveCapabilities);
  29. }catch(IllegalArgumentExceptionex){
  30. ......
  31. }
  32. /*Forchildprocess*/
  33. //新创建的子进程会执行handleSystemServerProcess函数。
  34. if(pid==0){
  35. handleSystemServerProcess(parsedArgs);
  36. }
  37. returntrue;
  38. }
  39. ......
  40. }
[java] view plain copy
  1. publicclassZygoteInit{
  2. ......
  3. privatestaticvoidhandleSystemServerProcess(
  4. ZygoteConnection.ArgumentsparsedArgs)
  5. throwsZygoteInit.MethodAndArgsCaller{

  6. //Zygote进程创建的子进程会继承Zygote进程创建的Socket文件描述符,而这里的子进程又不会用到它,因此,这里就调用closeServerSocket函数来关闭它。
  7. closeServerSocket();
  8. /*
  9. *PasstheremainingargumentstoSystemServer.
  10. *"--nice-name=system_servercom.android.server.SystemServer"
  11. */
  12. //进一步执行启动SystemServer组件的操作
  13. RuntimeInit.zygoteInit(parsedArgs.remainingArgs);
  14. /*shouldneverreachhere*/
  15. }
  16. ......
  17. }

函数定义在frameworks/base/core/java/com/android/internal/os/RuntimeInit.java文件中:

[java] view plain copy
  1. publicclassRuntimeInit{
  2. ......
  3. publicstaticfinalvoidzygoteInit(String[]argv)
  4. throwsZygoteInit.MethodAndArgsCaller{
  5. ......
  6. //执行一个Binder进程间通信机制的初始化工作,这个工作完成之后,这个进程中的Binder对象就可以方便地进行进程间通信了
  7. //函数zygoteInitNative是一个Native函数,实现在frameworks/base/core/jni/AndroidRuntime.cpp文件中,这里我们就不再细看了
  8. zygoteInitNative();
  9. ......
  10. //调用上面传进来的com.android.server.SystemServer类的main函数。
  11. //Remainingargumentsarepassedtothestartclass'sstaticmain
  12. StringstartClass=argv[curArg++];
  13. String[]startArgs=newString[argv.length-curArg];
  14. System.arraycopy(argv,curArg,startArgs,0,startArgs.length);
  15. invokeStaticMain(startClass,startArgs);
  16. }
  17. ......
  18. }

这个函数定义在frameworks/base/services/java/com/android/server/SystemServer.java文件中:

[java] view plain copy
  1. publicclassSystemServer
  2. {
  3. ......
  4. nativepublicstaticvoidinit1(String[]args);
  5. ......
  6. publicstaticvoidmain(String[]args){
  7. ......
  8. init1(args);//首先会执行JNI方法init1,然后init1会调用这里的init2函数
  9. ......
  10. }
  11. publicstaticfinalvoidinit2(){
  12. Slog.i(TAG,"EnteredtheAndroidsystemserver!");
  13. //创建一个ServerThread线程对象来执行一些系统关键服务的启动操作,
  14. //例如PackageManagerService和ActivityManagerService
  15. Threadthr=newServerThread();
  16. thr.setName("android.server.ServerThread");
  17. thr.start();
  18. }
  19. ......
  20. }

Step2 、 frameworks/base/services/jni/com_android_server_SystemServer.cpp文件中:

[cpp] view plain copy
  1. namespaceandroid{
  2. extern"C"intsystem_init();
  3. staticvoidandroid_server_SystemServer_init1(JNIEnv*env,jobjectclazz)
  4. {
  5. system_init();
  6. }
  7. /*
  8. *JNIregistration.
  9. */
  10. staticJNINativeMethodgMethods[]={
  11. /*name,signature,funcPtr*/
  12. {"init1","([Ljava/lang/String;)V",(void*)android_server_SystemServer_init1},
  13. };
  14. intregister_android_server_SystemServer(JNIEnv*env)
  15. {
  16. returnjniRegisterNativeMethods(env,"com/android/server/SystemServer",
  17. gMethods,NELEM(gMethods));
  18. }
  19. };//namespaceandroid

函数system_init实现在libsystem_server库中,源代码位于frameworks/base/cmds/system_server/library/system_init.cpp文件中:

[cpp] view plain copy
  1. extern"C"status_tsystem_init()
  2. {
  3. LOGI("Enteredsystem_init()");
  4. sp<ProcessState>proc(ProcessState::self());
  5. sp<IServiceManager>sm=defaultServiceManager();
  6. LOGI("ServiceManager:%p\n",sm.get());
  7. sp<GrimReaper>grim=newGrimReaper();
  8. sm->asBinder()->linkToDeath(grim,grim.get(),0);
  9. charpropBuf[PROPERTY_VALUE_MAX];
  10. property_get("system_init.startsurfaceflinger",propBuf,"1");
  11. if(strcmp(propBuf,"1")==0){
  12. //StarttheSurfaceFlinger
  13. SurfaceFlinger::instantiate();
  14. }
  15. //Startthesensorservice
  16. SensorService::instantiate();
  17. //Onthesimulator,audioflingeretaldon'tgetstartedthe
  18. //samewayasonthedevice,andweneedtostartthemhere
  19. if(!proc->supportsProcesses()){
  20. //StarttheAudioFlinger
  21. AudioFlinger::instantiate();
  22. //Startthemediaplaybackservice
  23. MediaPlayerService::instantiate();
  24. //Startthecameraservice
  25. CameraService::instantiate();
  26. //Starttheaudiopolicyservice
  27. AudioPolicyService::instantiate();
  28. }
  29. //AndnowstarttheAndroidruntime.Wehavetodothisbit
  30. //ofnastinessbecausetheAndroidruntimeinitializationrequires
  31. //someofthecoresystemservicestoalreadybestarted.
  32. //AllotherserversshouldjuststarttheAndroidruntimeat
  33. //thebeginningoftheirprocesses'smain(),beforecalling
  34. //theinitfunction.
  35. LOGI("Systemserver:startingAndroidruntime.\n");
  36. AndroidRuntime*runtime=AndroidRuntime::getRuntime();
  37. LOGI("Systemserver:startingAndroidservices.\n");
  38. runtime->callStatic("com/android/server/SystemServer","init2");
  39. //Ifrunninginourownprocess,justgointothethread
  40. //pool.Otherwise,calltheinitializationfinished
  41. //functoletthisprocesscontinueitsinitilization.
  42. if(proc->supportsProcesses()){
  43. LOGI("Systemserver:enteringthreadpool.\n");
  44. ProcessState::self()->startThreadPool();
  45. IPCThreadState::self()->joinThreadPool();
  46. LOGI("Systemserver:exitingthreadpool.\n");
  47. }
  48. returnNO_ERROR;
  49. }
这个函数首先会初始化SurfaceFlinger、SensorService、AudioFlinger、MediaPlayerService、CameraService和AudioPolicyService这几个服务,然后就通过系统全局唯一的AndroidRuntime实例变量runtime的callStatic来调用SystemServer的init2函数了。

这个函数定义在frameworks/base/core/jni/AndroidRuntime.cpp文件中:

[cpp] view plain copy
  1. /*
  2. *CallastaticJavaProgrammingLanguagefunctionthattakesnoargumentsandreturnsvoid.
  3. */
  4. status_tAndroidRuntime::callStatic(constchar*className,constchar*methodName)
  5. {
  6. JNIEnv*env;
  7. jclassclazz;
  8. jmethodIDmethodId;
  9. env=getJNIEnv();
  10. if(env==NULL)
  11. returnUNKNOWN_ERROR;
  12. clazz=findClass(env,className);
  13. if(clazz==NULL){
  14. LOGE("ERROR:couldnotfindclass'%s'\n",className);
  15. returnUNKNOWN_ERROR;
  16. }
  17. methodId=env->GetStaticMethodID(clazz,methodName,"()V");
  18. if(methodId==NULL){
  19. LOGE("ERROR:couldnotfindmethod%s.%s\n",className,methodName);
  20. returnUNKNOWN_ERROR;
  21. }
  22. env->CallStaticVoidMethod(clazz,methodId);
  23. returnNO_ERROR;
  24. }
这个函数调用由参数className指定的java类的静态成员函数,这个静态成员函数是由参数methodName指定的。上面传进来的参数className的值为"com/android/server/SystemServer",而参数methodName的值为"init2",因此,接下来就会调用SystemServer类的init2函数了。


Step3、

这个函数定义在frameworks/base/services/java/com/android/server/SystemServer.java文件中:

[java] view plain copy
  1. classServerThreadextendsThread{
  2. privatestaticfinalStringTAG="SystemServer";
  3. privatefinalstaticbooleanINCLUDE_DEMO=false;
  4. privatestaticfinalintLOG_BOOT_PROGRESS_SYSTEM_RUN=3010;
  5. privateContentResolvermContentResolver;
  6. privateclassAdbSettingsObserverextendsContentObserver{
  7. publicAdbSettingsObserver(){
  8. super(null);
  9. }
  10. @Override
  11. publicvoidonChange(booleanselfChange){
  12. booleanenableAdb=(Settings.Secure.getInt(mContentResolver,
  13. Settings.Secure.ADB_ENABLED,0)>0);
  14. //settingthissecurepropertywillstartorstopadbd
  15. SystemProperties.set("persist.service.adb.enable",enableAdb?"1":"0");
  16. }
  17. }
  18. @Override
  19. publicvoidrun(){
  20. EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN,
  21. SystemClock.uptimeMillis());
  22. Looper.prepare();
  23. android.os.Process.setThreadPriority(
  24. android.os.Process.THREAD_PRIORITY_FOREGROUND);
  25. BinderInternal.disableBackgroundScheduling(true);
  26. android.os.Process.setCanSelfBackground(false);
  27. //Checkwhetherwefailedtoshutdownlasttimewetried.
  28. {
  29. finalStringshutdownAction=SystemProperties.get(
  30. ShutdownThread.SHUTDOWN_ACTION_PROPERTY,"");
  31. if(shutdownAction!=null&&shutdownAction.length()>0){
  32. booleanreboot=(shutdownAction.charAt(0)=='1');
  33. finalStringreason;
  34. if(shutdownAction.length()>1){
  35. reason=shutdownAction.substring(1,shutdownAction.length());
  36. }else{
  37. reason=null;
  38. }
  39. ShutdownThread.rebootOrShutdown(reboot,reason);
  40. }
  41. }
  42. StringfactoryTestStr=SystemProperties.get("ro.factorytest");
  43. intfactoryTest="".equals(factoryTestStr)?SystemServer.FACTORY_TEST_OFF
  44. :Integer.parseInt(factoryTestStr);
  45. LightsServicelights=null;
  46. PowerManagerServicepower=null;
  47. BatteryServicebattery=null;
  48. ConnectivityServiceconnectivity=null;
  49. IPackageManagerpm=null;
  50. Contextcontext=null;
  51. WindowManagerServicewm=null;
  52. BluetoothServicebluetooth=null;
  53. BluetoothA2dpServicebluetoothA2dp=null;
  54. HeadsetObserverheadset=null;
  55. DockObserverdock=null;
  56. UsbServiceusb=null;
  57. UiModeManagerServiceuiMode=null;
  58. RecognitionManagerServicerecognition=null;
  59. ThrottleServicethrottle=null;
  60. //Criticalservices...
  61. try{
    //EntropyService服务
  62. Slog.i(TAG,"EntropyService");
  63. ServiceManager.addService("entropy",newEntropyService());
  64. //电源管理服务
  65. Slog.i(TAG,"PowerManager");
  66. power=newPowerManagerService();
  67. ServiceManager.addService(Context.POWER_SERVICE,power);
  68. //ActivityManagerService服务
  69. Slog.i(TAG,"ActivityManager");
  70. context=ActivityManagerService.main(factoryTest);

  71. //TelephonyRegistry服务
  72. Slog.i(TAG,"TelephonyRegistry");
  73. ServiceManager.addService("telephony.registry",newTelephonyRegistry(context));
  74. AttributeCache.init(context);
  75. //PackageManagerService包管理服务
  76. Slog.i(TAG,"PackageManager");
  77. pm=PackageManagerService.main(context,
  78. factoryTest!=SystemServer.FACTORY_TEST_OFF);
  79. ActivityManagerService.setSystemProcess();
  80. //内容解决者
  81. mContentResolver=context.getContentResolver();

  82. //账号、密码、权限管理服务
  83. //TheAccountManagermustcomebeforetheContentService
  84. try{
  85. Slog.i(TAG,"AccountManager");
  86. ServiceManager.addService(Context.ACCOUNT_SERVICE,
  87. newAccountManagerService(context));
  88. }catch(Throwablee){
  89. Slog.e(TAG,"FailurestartingAccountManager",e);
  90. }
  91. //内容服务
  92. Slog.i(TAG,"ContentManager");
  93. ContentService.main(context,
  94. factoryTest==SystemServer.FACTORY_TEST_LOW_LEVEL);

  95. //内容提供者
  96. Slog.i(TAG,"SystemContentProviders");
  97. ActivityManagerService.installSystemProviders();
  98. //电池服务
  99. Slog.i(TAG,"BatteryService");
  100. battery=newBatteryService(context);
  101. ServiceManager.addService("battery",battery);
  102. //亮屏服务
  103. Slog.i(TAG,"LightsService");
  104. lights=newLightsService(context);
  105. //手机震动服务
  106. Slog.i(TAG,"VibratorService");
  107. ServiceManager.addService("vibrator",newVibratorService(context));
  108. //在开始亮屏服务、内容提供者服务、电池服务启动后,才初始化电源服务
  109. //onlyinitializethepowerserviceafterwehavestartedthe
  110. //lightsservice,contentprovidersandthebatteryservice.
  111. power.init(context,lights,ActivityManagerService.getDefault(),battery);
  112. //闹钟服务
  113. Slog.i(TAG,"AlarmManager");
  114. AlarmManagerServicealarm=newAlarmManagerService(context);
  115. ServiceManager.addService(Context.ALARM_SERVICE,alarm);
  116. //初始化看门狗
  117. Slog.i(TAG,"InitWatchdog");
  118. Watchdog.getInstance().init(context,battery,power,alarm,
  119. ActivityManagerService.self());
  120. //窗口管理服务
  121. Slog.i(TAG,"WindowManager");
  122. wm=WindowManagerService.main(context,power,
  123. factoryTest!=SystemServer.FACTORY_TEST_LOW_LEVEL);
  124. ServiceManager.addService(Context.WINDOW_SERVICE,wm);
  125. ((ActivityManagerService)ServiceManager.getService("activity"))
  126. .setWindowManager(wm);

  127. //蓝牙服务
  128. //SkipBluetoothifwehaveanemulatorkernel
  129. //TODO:Useamorereliablechecktoseeifthisproductshould
  130. //supportBluetooth-seebug988521
  131. if(SystemProperties.get("ro.kernel.qemu").equals("1")){
  132. Slog.i(TAG,"RegisteringnullBluetoothService(emulator)");
  133. ServiceManager.addService(BluetoothAdapter.BLUETOOTH_SERVICE,null);
  134. }elseif(factoryTest==SystemServer.FACTORY_TEST_LOW_LEVEL){
  135. Slog.i(TAG,"RegisteringnullBluetoothService(factorytest)");
  136. ServiceManager.addService(BluetoothAdapter.BLUETOOTH_SERVICE,null);
  137. }else{
  138. Slog.i(TAG,"BluetoothService");
  139. bluetooth=newBluetoothService(context);
  140. ServiceManager.addService(BluetoothAdapter.BLUETOOTH_SERVICE,bluetooth);
  141. bluetooth.initAfterRegistration();
  142. bluetoothA2dp=newBluetoothA2dpService(context,bluetooth);
  143. ServiceManager.addService(BluetoothA2dpService.BLUETOOTH_A2DP_SERVICE,
  144. bluetoothA2dp);
  145. intbluetoothOn=Settings.Secure.getInt(mContentResolver,
  146. Settings.Secure.BLUETOOTH_ON,0);
  147. if(bluetoothOn>0){
  148. bluetooth.enable();
  149. }
  150. }
  151. }catch(RuntimeExceptione){
  152. Slog.e("System","Failurestartingcoreservice",e);
  153. }
  154. DevicePolicyManagerServicedevicePolicy=null;
  155. StatusBarManagerServicestatusBar=null;
  156. InputMethodManagerServiceimm=null;
  157. AppWidgetServiceappWidget=null;
  158. NotificationManagerServicenotification=null;
  159. WallpaperManagerServicewallpaper=null;
  160. LocationManagerServicelocation=null;
  161. if(factoryTest!=SystemServer.FACTORY_TEST_LOW_LEVEL){
  162. try{
  163. Slog.i(TAG,"DevicePolicy");
  164. devicePolicy=newDevicePolicyManagerService(context);
  165. ServiceManager.addService(Context.DEVICE_POLICY_SERVICE,devicePolicy);
  166. }catch(Throwablee){
  167. Slog.e(TAG,"FailurestartingDevicePolicyService",e);
  168. }
  169. try{
  170. Slog.i(TAG,"StatusBar");
  171. statusBar=newStatusBarManagerService(context);
  172. ServiceManager.addService(Context.STATUS_BAR_SERVICE,statusBar);
  173. }catch(Throwablee){
  174. Slog.e(TAG,"FailurestartingStatusBarManagerService",e);
  175. }
  176. try{
  177. Slog.i(TAG,"ClipboardService");
  178. ServiceManager.addService(Context.CLIPBOARD_SERVICE,
  179. newClipboardService(context));
  180. }catch(Throwablee){
  181. Slog.e(TAG,"FailurestartingClipboardService",e);
  182. }
  183. //输入法管理服务
  184. try{
  185. Slog.i(TAG,"InputMethodService");
  186. imm=newInputMethodManagerService(context,statusBar);
  187. ServiceManager.addService(Context.INPUT_METHOD_SERVICE,imm);
  188. }catch(Throwablee){
  189. Slog.e(TAG,"FailurestartingInputManagerService",e);
  190. }
  191. //网络状态服务
  192. try{
  193. Slog.i(TAG,"NetStatService");
  194. ServiceManager.addService("netstat",newNetStatService(context));
  195. }catch(Throwablee){
  196. Slog.e(TAG,"FailurestartingNetStatService",e);
  197. }
  198. //网络管理服务
  199. try{
  200. Slog.i(TAG,"NetworkManagementService");
  201. ServiceManager.addService(
  202. Context.NETWORKMANAGEMENT_SERVICE,
  203. NetworkManagementService.create(context));
  204. }catch(Throwablee){
  205. Slog.e(TAG,"FailurestartingNetworkManagementService",e);
  206. }
  207. //网络连接服务
  208. try{
  209. Slog.i(TAG,"ConnectivityService");
  210. connectivity=ConnectivityService.getInstance(context);
  211. ServiceManager.addService(Context.CONNECTIVITY_SERVICE,connectivity);
  212. }catch(Throwablee){
  213. Slog.e(TAG,"FailurestartingConnectivityService",e);
  214. }
  215. //节流阀控制,提高modem使用效率,避免数据过大或过小情况下不必要的重复连接和断开。
  216. try{
  217. Slog.i(TAG,"ThrottleService");
  218. throttle=newThrottleService(context);
  219. ServiceManager.addService(
  220. Context.THROTTLE_SERVICE,throttle);
  221. }catch(Throwablee){
  222. Slog.e(TAG,"FailurestartingThrottleService",e);
  223. }

  224. //可访问管理服务
  225. try{
  226. Slog.i(TAG,"AccessibilityManager");
  227. ServiceManager.addService(Context.ACCESSIBILITY_SERVICE,
  228. newAccessibilityManagerService(context));
  229. }catch(Throwablee){
  230. Slog.e(TAG,"FailurestartingAccessibilityManager",e);
  231. }
  232. //磁盘加载服务(sd卡等)
  233. try{
  234. /*
  235. *NotificationManagerServiceisdependantonMountService,
  236. *(formedia/usbnotifications)sowemuststartMountServicefirst.
  237. */
  238. Slog.i(TAG,"MountService");
  239. ServiceManager.addService("mount",newMountService(context));
  240. }catch(Throwablee){
  241. Slog.e(TAG,"FailurestartingMountService",e);
  242. }
  243. //通知管理服务
  244. try{
  245. Slog.i(TAG,"NotificationManager");
  246. notification=newNotificationManagerService(context,statusBar,lights);
  247. ServiceManager.addService(Context.NOTIFICATION_SERVICE,notification);
  248. }catch(Throwablee){
  249. Slog.e(TAG,"FailurestartingNotificationManager",e);
  250. }
  251. //监控磁盘空间服务
  252. try{
  253. Slog.i(TAG,"DeviceStorageMonitor");
  254. ServiceManager.addService(DeviceStorageMonitorService.SERVICE,
  255. newDeviceStorageMonitorService(context));
  256. }catch(Throwablee){
  257. Slog.e(TAG,"FailurestartingDeviceStorageMonitorservice",e);
  258. }
  259. //定位管理服务
  260. try{
  261. Slog.i(TAG,"LocationManager");
  262. location=newLocationManagerService(context);
  263. ServiceManager.addService(Context.LOCATION_SERVICE,location);
  264. }catch(Throwablee){
  265. Slog.e(TAG,"FailurestartingLocationManager",e);
  266. }
  267. //搜索管理服务
  268. try{
  269. Slog.i(TAG,"SearchService");
  270. ServiceManager.addService(Context.SEARCH_SERVICE,
  271. newSearchManagerService(context));
  272. }catch(Throwablee){
  273. Slog.e(TAG,"FailurestartingSearchService",e);
  274. }
  275. //开启守护进程
  276. if(INCLUDE_DEMO){
  277. Slog.i(TAG,"Installingdemodata...");
  278. (newDemoThread(context)).start();
  279. }
  280. //DropBoxManagerService用于生成和管理系统运行时的一些日志文件。这些日志文件大多记录的是系统或某个应用程序出错时的信息。
  281. try{
  282. Slog.i(TAG,"DropBoxService");
  283. ServiceManager.addService(Context.DROPBOX_SERVICE,
  284. newDropBoxManagerService(context,newFile("/data/system/dropbox")));
  285. }catch(Throwablee){
  286. Slog.e(TAG,"FailurestartingDropBoxManagerService",e);
  287. }
  288. //壁纸管理服务
  289. try{
  290. Slog.i(TAG,"WallpaperService");
  291. wallpaper=newWallpaperManagerService(context);
  292. ServiceManager.addService(Context.WALLPAPER_SERVICE,wallpaper);
  293. }catch(Throwablee){
  294. Slog.e(TAG,"FailurestartingWallpaperService",e);
  295. }
  296. //音频服务
  297. try{
  298. Slog.i(TAG,"AudioService");
  299. ServiceManager.addService(Context.AUDIO_SERVICE,newAudioService(context));
  300. }catch(Throwablee){
  301. Slog.e(TAG,"FailurestartingAudioService",e);
  302. }

  303. //HeadsetObserver耳机监听
  304. try{
  305. Slog.i(TAG,"HeadsetObserver");
  306. //Listenforwiredheadsetchanges
  307. headset=newHeadsetObserver(context);
  308. }catch(Throwablee){
  309. Slog.e(TAG,"FailurestartingHeadsetObserver",e);
  310. }

  311. //DockObserver手机亮度的监听
  312. try{
  313. Slog.i(TAG,"DockObserver");
  314. //Listenfordockstationchanges
  315. dock=newDockObserver(context,power);
  316. }catch(Throwablee){
  317. Slog.e(TAG,"FailurestartingDockObserver",e);
  318. }
  319. //usb服务
  320. try{
  321. Slog.i(TAG,"USBService");
  322. //ListenforUSBchanges
  323. usb=newUsbService(context);
  324. ServiceManager.addService(Context.USB_SERVICE,usb);
  325. }catch(Throwablee){
  326. Slog.e(TAG,"FailurestartingUsbService",e);
  327. }
  328. //ui模式管理服务
  329. try{
  330. Slog.i(TAG,"UIModeManagerService");
  331. //ListenforUImodechanges
  332. uiMode=newUiModeManagerService(context);
  333. }catch(Throwablee){
  334. Slog.e(TAG,"FailurestartingUiModeManagerService",e);
  335. }

  336. //备份管理服务
  337. try{
  338. Slog.i(TAG,"BackupService");
  339. ServiceManager.addService(Context.BACKUP_SERVICE,
  340. newBackupManagerService(context));
  341. }catch(Throwablee){
  342. Slog.e(TAG,"FailurestartingBackupService",e);
  343. }
  344. //appWidget服务
  345. try{
  346. Slog.i(TAG,"AppWidgetService");
  347. appWidget=newAppWidgetService(context);
  348. ServiceManager.addService(Context.APPWIDGET_SERVICE,appWidget);
  349. }catch(Throwablee){
  350. Slog.e(TAG,"FailurestartingAppWidgetService",e);
  351. }
  352. try{
  353. Slog.i(TAG,"RecognitionService");
  354. recognition=newRecognitionManagerService(context);
  355. }catch(Throwablee){
  356. Slog.e(TAG,"FailurestartingRecognitionService",e);
  357. }
  358. try{
  359. Slog.i(TAG,"DiskStatsService");
  360. ServiceManager.addService("diskstats",newDiskStatsService(context));
  361. }catch(Throwablee){
  362. Slog.e(TAG,"FailurestartingDiskStatsService",e);
  363. }
  364. }
  365. //makesuretheADB_ENABLEDsettingvaluematchesthesecurepropertyvalue
  366. Settings.Secure.putInt(mContentResolver,Settings.Secure.ADB_ENABLED,
  367. "1".equals(SystemProperties.get("persist.service.adb.enable"))?1:0);
  368. //registerobservertolistenforsettingschanges
  369. mContentResolver.registerContentObserver(Settings.Secure.getUriFor(Settings.Secure.ADB_ENABLED),
  370. false,newAdbSettingsObserver());
  371. //Beforethingsstartrolling,besurewehavedecidedwhether
  372. //weareinsafemode.
  373. finalbooleansafeMode=wm.detectSafeMode();
  374. if(safeMode){
  375. try{
  376. ActivityManagerNative.getDefault().enterSafeMode();
  377. //PostthesafemodestateintheZygoteclass
  378. Zygote.systemInSafeMode=true;
  379. //DisabletheJITforthesystem_serverprocess
  380. VMRuntime.getRuntime().disableJitCompilation();
  381. }catch(RemoteExceptione){
  382. }
  383. }else{
  384. //EnabletheJITforthesystem_serverprocess
  385. VMRuntime.getRuntime().startJitCompilation();
  386. }
  387. //Itisnowtimetostartuptheappprocesses...
  388. if(devicePolicy!=null){
  389. devicePolicy.systemReady();
  390. }
  391. if(notification!=null){
  392. notification.systemReady();
  393. }
  394. if(statusBar!=null){
  395. statusBar.systemReady();
  396. }
  397. wm.systemReady();
  398. power.systemReady();
  399. try{
  400. pm.systemReady();
  401. }catch(RemoteExceptione){
  402. }
  403. //Theseareneededtopropagatetotherunnablebelow.
  404. finalStatusBarManagerServicestatusBarF=statusBar;
  405. finalBatteryServicebatteryF=battery;
  406. finalConnectivityServiceconnectivityF=connectivity;
  407. finalDockObserverdockF=dock;
  408. finalUsbServiceusbF=usb;
  409. finalThrottleServicethrottleF=throttle;
  410. finalUiModeManagerServiceuiModeF=uiMode;
  411. finalAppWidgetServiceappWidgetF=appWidget;
  412. finalWallpaperManagerServicewallpaperF=wallpaper;
  413. finalInputMethodManagerServiceimmF=imm;
  414. finalRecognitionManagerServicerecognitionF=recognition;
  415. finalLocationManagerServicelocationF=location;
  416. //Wenowtelltheactivitymanageritisokaytorunthirdparty
  417. //code.Itwillcallbackintousonceithasgottentothestate
  418. //wherethirdpartycodecanreallyrun(butbeforeithasactually
  419. //startedlaunchingtheinitialapplications),forustocompleteour
  420. //initialization.
  421. ((ActivityManagerService)ActivityManagerNative.getDefault())
  422. .systemReady(newRunnable(){
  423. publicvoidrun(){
  424. Slog.i(TAG,"Makingservicesready");
  425. if(statusBarF!=null)statusBarF.systemReady2();
  426. if(batteryF!=null)batteryF.systemReady();
  427. if(connectivityF!=null)connectivityF.systemReady();
  428. if(dockF!=null)dockF.systemReady();
  429. if(usbF!=null)usbF.systemReady();
  430. if(uiModeF!=null)uiModeF.systemReady();
  431. if(recognitionF!=null)recognitionF.systemReady();
  432. Watchdog.getInstance().start();
  433. //Itisnowokaytoletthevarioussystemservicesstarttheir
  434. //thirdpartycode...
  435. if(appWidgetF!=null)appWidgetF.systemReady(safeMode);
  436. if(wallpaperF!=null)wallpaperF.systemReady();
  437. if(immF!=null)immF.systemReady();
  438. if(locationF!=null)locationF.systemReady();
  439. if(throttleF!=null)throttleF.systemReady();
  440. }
  441. });
  442. //Fordebugbuilds,logeventloopstallstodropboxforanalysis.
  443. if(StrictMode.conditionallyEnableDebugLogging()){
  444. Slog.i(TAG,"EnabledStrictModeforsystemservermainthread.");
  445. }
  446. Looper.loop();
  447. Slog.d(TAG,"SystemServerThreadisexiting!");
  448. }
  449. }


这个函数除了启动了很多系统服务,自己慢慢研究









更多相关文章

  1. Android Service 中 onStartCommand()函数返回值含义
  2. android使用sharedPreferences()方法读写文件操作
  3. 在系统里设置文件默认打开APP
  4. Android实现屏幕截图并保存截图到指定文件
  5. Android 中读取SD卡文件时抛出NullPointerException错误解决办法
  6. Android media媒体库分析之:分类别统计媒体文件大小

随机推荐

  1. Jupyter Notebook & Lab快捷键大全
  2. 掌握pandas中的transform
  3. Vaex :突破pandas,快速分析100GB大数据集
  4. 纯Python绘制满满艺术感的山脊地图
  5. 来,一起打卡!
  6. JavaScript中的持续传递风格 [每日前端夜
  7. 【开发者必看】移动应用趋势洞察白皮书-
  8. 在模仿中精进数据可视化03:OD数据的特殊可
  9. 在模仿中精进数据可视化02:温室气体排放来
  10. 在pandas中使用数据透视表