现在的位置: 首页 > 综合 > 正文

Android系统SystemServer进程启动过程源码分析

2018年05月01日 ⁄ 综合 ⁄ 共 29432字 ⁄ 字号 评论关闭

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

Step1、startSystemServer函数

[java] view
plain
copy在CODE上查看代码片派生到我的代码片

  1. public class ZygoteInit {  
  2.     ......  
  3.   
  4.     private static boolean startSystemServer()  
  5.             throws MethodAndArgsCaller, RuntimeException {  
  6.         /* Hardcoded command line to start the system server */  
  7.         String args[] = {  
  8.             "--setuid=1000",  
  9.             "--setgid=1000",  
  10.             "--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,3001,3002,3003",  
  11.             "--capabilities=130104352,130104352",  
  12.             "--runtime-init",  
  13.             "--nice-name=system_server",  
  14.             "com.android.server.SystemServer",  
  15.         };  
  16.         ZygoteConnection.Arguments parsedArgs = null;  
  17.   
  18.         int pid;  
  19.   
  20.         try {  
  21.             parsedArgs = new ZygoteConnection.Arguments(args);  
  22.   
  23.             ......  
  24.   
  25.             /* Request to fork the system server process */ 
  26. //startSystemServer()调用Zygote的native方法
    forkSystemServer(),java端的Zygote的准备工作就结束了,接下来就交给C/C++端的Zygote来执行fork任务了,在/dalvik/vm/native/dalvik_system_Zygote.c
    中,Dalvik_dalvik_system_Zygote_forkSystemServer函数中(自己去看源码)
  27. 这个里面的fork进程主要是使用linux的fork进程
  28. //Zygote进程通过Zygote.forkSystemServer函数来创建一个新的进程来启动SystemServer组件
  29.             pid = Zygote.forkSystemServer(  
  30.                 parsedArgs.uid, parsedArgs.gid,  
  31.                 parsedArgs.gids, debugFlags, null,  
  32.                 parsedArgs.permittedCapabilities,  
  33.                 parsedArgs.effectiveCapabilities);  
  34.         } catch (IllegalArgumentException ex) {  
  35.             ......  
  36.         }  
  37.   
  38.         /* For child process */ 
  39. //新创建的子进程会执行handleSystemServerProcess函数。 
  40.         if (pid == 0) {  
  41.             handleSystemServerProcess(parsedArgs);  
  42.         }  
  43.   
  44.         return true;  
  45.     }  
  46.       
  47.     ......  
  48. }  

  

[java] view
plain
copy在CODE上查看代码片派生到我的代码片

  1. public class ZygoteInit {  
  2.     ......  
  3.   
  4.     private static void handleSystemServerProcess(  
  5.             ZygoteConnection.Arguments parsedArgs)  
  6.             throws ZygoteInit.MethodAndArgsCaller { 

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

       

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

[java] view
plain
copy在CODE上查看代码片派生到我的代码片

  1. public class RuntimeInit {    
  2.     ......    
  3.   
  4.     public static final void zygoteInit(String[] argv)    
  5.             throws ZygoteInit.MethodAndArgsCaller {    
  6.         ......    
  7. //执行一个Binder进程间通信机制的初始化工作,这个工作完成之后,这个进程中的Binder对象就可以方便地进行进程间通信了
  8. //函数zygoteInitNative是一个Native函数,实现在frameworks/base/core/jni/AndroidRuntime.cpp文件中,这里我们就不再细看了
  9.         zygoteInitNative();    
  10.   
  11.         ......    
  12.   
  13. //调用上面传进来的com.android.server.SystemServer类的main函数。
  14.         // Remaining arguments are passed to the start class's static main    
  15.   
  16.         String startClass = argv[curArg++];    
  17.         String[] startArgs = new String[argv.length - curArg];    
  18.   
  19.         System.arraycopy(argv, curArg, startArgs, 0, startArgs.length);    
  20.         invokeStaticMain(startClass, startArgs);    
  21.     }    
  22.   
  23.     ......    
  24. }  

        

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

[java] view
plain
copy在CODE上查看代码片派生到我的代码片

  1. public class SystemServer    
  2. {    
  3.     ......    
  4.   
  5.     native public static void init1(String[] args);    
  6.   
  7.     ......    
  8.   
  9.     public static void main(String[] args) {    
  10.         ......    
  11.   
  12.         init1(args);//首先会执行JNI方法init1,然后init1会调用这里的init2函数   
  13.   
  14.         ......    
  15.     }   
  16.   
  17.     public static final void init2() {    
  18.         Slog.i(TAG, "Entered the Android system server!"); 
  19. //创建一个ServerThread线程对象来执行一些系统关键服务的启动操作,
  20. //例如 PackageManagerService和ActivityManagerService  
  21.         Thread thr = new ServerThread();    
  22.         thr.setName("android.server.ServerThread");    
  23.         thr.start();    
  24.     }    
  25.   
  26.     ......    
  27. }  

    

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

  1. namespace android {  
  2.   
  3. extern "C" int system_init();  
  4.   
  5. static void android_server_SystemServer_init1(JNIEnv* env, jobject clazz)  
  6. {  
  7.     system_init();  
  8. }  
  9.   
  10. /* 
  11.  * JNI registration. 
  12.  */  
  13. static JNINativeMethod gMethods[] = {  
  14.     /* name, signature, funcPtr */  
  15.     { "init1""([Ljava/lang/String;)V", (void*) android_server_SystemServer_init1 },  
  16. };  
  17.   
  18. int register_android_server_SystemServer(JNIEnv* env)  
  19. {  
  20.     return jniRegisterNativeMethods(env, "com/android/server/SystemServer",  
  21.             gMethods, NELEM(gMethods));  
  22. }  
  23.   
  24. }; // namespace android  

 

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

  1. extern "C" status_t system_init()  
  2. {  
  3.     LOGI("Entered system_init()");  
  4.   
  5.     sp<ProcessState> proc(ProcessState::self());  
  6.   
  7.     sp<IServiceManager> sm = defaultServiceManager();  
  8.     LOGI("ServiceManager: %p\n", sm.get());  
  9.   
  10.     sp<GrimReaper> grim = new GrimReaper();  
  11.     sm->asBinder()->linkToDeath(grim, grim.get(), 0);  
  12.   
  13.     char propBuf[PROPERTY_VALUE_MAX];  
  14.     property_get("system_init.startsurfaceflinger", propBuf, "1");  
  15.     if (strcmp(propBuf, "1") == 0) {  
  16.         // Start the SurfaceFlinger  
  17.         SurfaceFlinger::instantiate();  
  18.     }  
  19.   
  20.     // Start the sensor service  
  21.     SensorService::instantiate();  
  22.   
  23.     // On the simulator, audioflinger et al don't get started the  
  24.     // same way as on the device, and we need to start them here  
  25.     if (!proc->supportsProcesses()) {  
  26.   
  27.         // Start the AudioFlinger  
  28.         AudioFlinger::instantiate();  
  29.   
  30.         // Start the media playback service  
  31.         MediaPlayerService::instantiate();  
  32.   
  33.         // Start the camera service  
  34.         CameraService::instantiate();  
  35.   
  36.         // Start the audio policy service  
  37.         AudioPolicyService::instantiate();  
  38.     }  
  39.   
  40.     // And now start the Android runtime.  We have to do this bit  
  41.     // of nastiness because the Android runtime initialization requires  
  42.     // some of the core system services to already be started.  
  43.     // All other servers should just start the Android runtime at  
  44.     // the beginning of their processes's main(), before calling  
  45.     // the init function.  
  46.     LOGI("System server: starting Android runtime.\n");  
  47.   
  48.     AndroidRuntime* runtime = AndroidRuntime::getRuntime();  
  49.   
  50.     LOGI("System server: starting Android services.\n");  
  51.     runtime->callStatic("com/android/server/SystemServer""init2");  
  52.   
  53.     // If running in our own process, just go into the thread  
  54.     // pool.  Otherwise, call the initialization finished  
  55.     // func to let this process continue its initilization.  
  56.     if (proc->supportsProcesses()) {  
  57.         LOGI("System server: entering thread pool.\n");  
  58.         ProcessState::self()->startThreadPool();  
  59.         IPCThreadState::self()->joinThreadPool();  
  60.         LOGI("System server: exiting thread pool.\n");  
  61.     }  
  62.   
  63.     return NO_ERROR;  
  64. }  

        这个函数首先会初始化SurfaceFlinger、SensorService、AudioFlinger、MediaPlayerService、CameraService和AudioPolicyService这几个服务,然后就通过系统全局唯一的AndroidRuntime实例变量runtime的callStatic来调用SystemServer的init2函数了。

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

  1. /* 
  2. * Call a static Java Programming Language function that takes no arguments and returns void. 
  3. */  
  4. status_t AndroidRuntime::callStatic(const char* className, const char* methodName)  
  5. {  
  6.     JNIEnv* env;  
  7.     jclass clazz;  
  8.     jmethodID methodId;  
  9.   
  10.     env = getJNIEnv();  
  11.     if (env == NULL)  
  12.         return UNKNOWN_ERROR;  
  13.   
  14.     clazz = findClass(env, className);  
  15.     if (clazz == NULL) {  
  16.         LOGE("ERROR: could not find class '%s'\n", className);  
  17.         return UNKNOWN_ERROR;  
  18.     }  
  19.     methodId = env->GetStaticMethodID(clazz, methodName, "()V");  
  20.     if (methodId == NULL) {  
  21.         LOGE("ERROR: could not find method %s.%s\n", className, methodName);  
  22.         return UNKNOWN_ERROR;  
  23.     }  
  24.   
  25.     env->CallStaticVoidMethod(clazz, methodId);  
  26.   
  27.     return NO_ERROR;  
  28. }  

        这个函数调用由参数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. class ServerThread extends Thread {  
  2.     private static final String TAG = "SystemServer";  
  3.     private final static boolean INCLUDE_DEMO = false;  
  4.   
  5.     private static final int LOG_BOOT_PROGRESS_SYSTEM_RUN = 3010;  
  6.   
  7.     private ContentResolver mContentResolver;  
  8.   
  9.     private class AdbSettingsObserver extends ContentObserver {  
  10.         public AdbSettingsObserver() {  
  11.             super(null);  
  12.         }  
  13.         @Override  
  14.         public void onChange(boolean selfChange) {  
  15.             boolean enableAdb = (Settings.Secure.getInt(mContentResolver,  
  16.                 Settings.Secure.ADB_ENABLED, 0) > 0);  
  17.             // setting this secure property will start or stop adbd  
  18.            SystemProperties.set("persist.service.adb.enable", enableAdb ? "1" : "0");  
  19.         }  
  20.     }  
  21.   
  22.     @Override  
  23.     public void run() {  
  24.         EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN,  
  25.             SystemClock.uptimeMillis());  
  26.   
  27.         Looper.prepare();  
  28.   
  29.         android.os.Process.setThreadPriority(  
  30.                 android.os.Process.THREAD_PRIORITY_FOREGROUND);  
  31.   
  32.         BinderInternal.disableBackgroundScheduling(true);  
  33.         android.os.Process.setCanSelfBackground(false);  
  34.   
  35.         // Check whether we failed to shut down last time we tried.  
  36.         {  
  37.             final String shutdownAction = SystemProperties.get(  
  38.                     ShutdownThread.SHUTDOWN_ACTION_PROPERTY, "");  
  39.             if (shutdownAction != null && shutdownAction.length() > 0) {  
  40.                 boolean reboot = (shutdownAction.charAt(0) == '1');  
  41.   
  42.                 final String reason;  
  43.                 if (shutdownAction.length() > 1) {  
  44.                     reason = shutdownAction.substring(1, shutdownAction.length());  
  45.                 } else {  
  46.                     reason = null;  
  47.                 }  
  48.   
  49.                 ShutdownThread.rebootOrShutdown(reboot, reason);  
  50.             }  
  51.         }  
  52.   
  53.         String factoryTestStr = SystemProperties.get("ro.factorytest");  
  54.         int factoryTest = "".equals(factoryTestStr) ? SystemServer.FACTORY_TEST_OFF  
  55.                 : Integer.parseInt(factoryTestStr);  
  56.   
  57.         LightsService lights = null;  
  58.         PowerManagerService power = null;  
  59.         BatteryService battery = null;  
  60.         ConnectivityService connectivity = null;  
  61.         IPackageManager pm = null;  
  62.         Context context = null;  
  63.         WindowManagerService wm = null;  
  64.         BluetoothService bluetooth = null;  
  65.         BluetoothA2dpService bluetoothA2dp = null;  
  66.         HeadsetObserver headset = null;  
  67.         DockObserver dock = null;  
  68.         UsbService usb = null;  
  69.         UiModeManagerService uiMode = null;  
  70.         RecognitionManagerService recognition = null;  
  71.         ThrottleService throttle = null;  
  72.   
  73.         // Critical services...  
  74.         try { 
    //EntropyService服务
  75.             Slog.i(TAG, "Entropy Service");  
  76.             ServiceManager.addService("entropy"new EntropyService());  
  77.   
  78. //电源管理服务
  79.             Slog.i(TAG, "Power Manager");  
  80.             power = new PowerManagerService();  
  81.             ServiceManager.addService(Context.POWER_SERVICE, power);  
  82.   
  83. //ActivityManagerService服务
  84.             Slog.i(TAG, "Activity Manager");  
  85.             context = ActivityManagerService.main(factoryTest);  

  86. //TelephonyRegistry服务
  87.             Slog.i(TAG, "Telephony Registry");  
  88.             ServiceManager.addService("telephony.registry"new TelephonyRegistry(context));  
  89.   
  90.             AttributeCache.init(context);  
  91.  
  92. //PackageManagerService包管理服务 
  93.             Slog.i(TAG, "Package Manager");  
  94.             pm = PackageManagerService.main(context,  
  95.                     factoryTest != SystemServer.FACTORY_TEST_OFF);  
  96.   
  97.             ActivityManagerService.setSystemProcess();  
  98.   
  99. //内容解决者
  100.             mContentResolver = context.getContentResolver();  

  101. //账号、密码、权限管理服务  
  102.             // The AccountManager must come before the ContentService  
  103.             try {  
  104.                 Slog.i(TAG, "Account Manager");  
  105.                 ServiceManager.addService(Context.ACCOUNT_SERVICE,  
  106.                         new AccountManagerService(context));  
  107.             } catch (Throwable e) {  
  108.                 Slog.e(TAG, "Failure starting Account Manager", e);  
  109.             }  
  110.   
  111. //内容服务
  112.             Slog.i(TAG, "Content Manager");  
  113.             ContentService.main(context,  
  114.                     factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL);  

  115. //内容提供者  
  116.             Slog.i(TAG, "System Content Providers");  
  117.             ActivityManagerService.installSystemProviders();  
  118.   
  119. //电池服务
  120.             Slog.i(TAG, "Battery Service");  
  121.             battery = new BatteryService(context);  
  122.             ServiceManager.addService("battery", battery);  
  123.   
  124. //亮屏服务
  125.             Slog.i(TAG, "Lights Service");  
  126.             lights = new LightsService(context);  
  127.   
  128. //手机震动服务
  129.             Slog.i(TAG, "Vibrator Service");  
  130.             ServiceManager.addService("vibrator"new VibratorService(context));  
  131.   
  132. //在开始亮屏服务、内容提供者服务、电池服务启动后,才初始化电源服务
  133.             // only initialize the power service after we have started the  
  134.             // lights service, content providers and the battery service.  
  135.             power.init(context, lights, ActivityManagerService.getDefault(), battery);  
  136.   
  137. //闹钟服务
  138.             Slog.i(TAG, "Alarm Manager");  
  139.             AlarmManagerService alarm = new AlarmManagerService(context);  
  140.             ServiceManager.addService(Context.ALARM_SERVICE, alarm);  
  141.   
  142. //初始化看门狗
  143.             Slog.i(TAG, "Init Watchdog");  
  144.             Watchdog.getInstance().init(context, battery, power, alarm,  
  145.                     ActivityManagerService.self());  
  146.   
  147. //窗口管理服务
  148.             Slog.i(TAG, "Window Manager");  
  149.             wm = WindowManagerService.main(context, power,  
  150.                     factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL);  
  151.             ServiceManager.addService(Context.WINDOW_SERVICE, wm);  
  152.   
  153.             ((ActivityManagerService)ServiceManager.getService("activity"))  
  154.                     .setWindowManager(wm);  

  155. //蓝牙服务  
  156.             // Skip Bluetooth if we have an emulator kernel  
  157.             // TODO: Use a more reliable check to see if this product should  
  158.             // support Bluetooth - see bug 988521  
  159.             if (SystemProperties.get("ro.kernel.qemu").equals("1")) {  
  160.                 Slog.i(TAG, "Registering null Bluetooth Service (emulator)");  
  161.                 ServiceManager.addService(BluetoothAdapter.BLUETOOTH_SERVICE, null);  
  162.             } else if (factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) {  
  163.                 Slog.i(TAG, "Registering null Bluetooth Service (factory test)");  
  164.                 ServiceManager.addService(BluetoothAdapter.BLUETOOTH_SERVICE, null);  
  165.             } else {  
  166.                 Slog.i(TAG, "Bluetooth Service");  
  167.                 bluetooth = new BluetoothService(context);  
  168.                 ServiceManager.addService(BluetoothAdapter.BLUETOOTH_SERVICE, bluetooth);  
  169.                 bluetooth.initAfterRegistration();  
  170.                 bluetoothA2dp = new BluetoothA2dpService(context, bluetooth);  
  171.                 ServiceManager.addService(BluetoothA2dpService.BLUETOOTH_A2DP_SERVICE,  
  172.                                           bluetoothA2dp);  
  173.   
  174.                 int bluetoothOn = Settings.Secure.getInt(mContentResolver,  
  175.                     Settings.Secure.BLUETOOTH_ON, 0);  
  176.                 if (bluetoothOn > 0) {  
  177.                     bluetooth.enable();  
  178.                 }  
  179.             }  
  180.   
  181.         } catch (RuntimeException e) {  
  182.             Slog.e("System""Failure starting core service", e);  
  183.         }  
  184.   
  185.         DevicePolicyManagerService devicePolicy = null;  
  186.         StatusBarManagerService statusBar = null;  
  187.         InputMethodManagerService imm = null;  
  188.         AppWidgetService appWidget = null;  
  189.         NotificationManagerService notification = null;  
  190.         WallpaperManagerService wallpaper = null;  
  191.         LocationManagerService location = null;  
  192.   
  193.         if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {  
  194.             try {  
  195.                 Slog.i(TAG, "Device Policy");  
  196.                 devicePolicy = new DevicePolicyManagerService(context);  
  197.                 ServiceManager.addService(Context.DEVICE_POLICY_SERVICE, devicePolicy);  
  198.             } catch (Throwable e) {  
  199.                 Slog.e(TAG, "Failure starting DevicePolicyService", e);  
  200.             }  
  201.   
  202.             try {  
  203.                 Slog.i(TAG, "Status Bar");  
  204.                 statusBar = new StatusBarManagerService(context);  
  205.                 ServiceManager.addService(Context.STATUS_BAR_SERVICE, statusBar);  
  206.             } catch (Throwable e) {  
  207.                 Slog.e(TAG, "Failure starting StatusBarManagerService", e);  
  208.             }  
  209.   
  210.             try {  
  211.                 Slog.i(TAG, "Clipboard Service");  
  212.                 ServiceManager.addService(Context.CLIPBOARD_SERVICE,  
  213.                         new ClipboardService(context));  
  214.             } catch (Throwable e) {  
  215.                 Slog.e(TAG, "Failure starting Clipboard Service", e);  
  216.             }  
  217.  //输入法管理服务
  218.             try {  
  219.                 Slog.i(TAG, "Input Method Service");  
  220.                 imm = new InputMethodManagerService(context, statusBar);  
  221.                 ServiceManager.addService(Context.INPUT_METHOD_SERVICE, imm);  
  222.             } catch (Throwable e) {  
  223.                 Slog.e(TAG, "Failure starting Input Manager Service", e);  
  224.             }  
  225.   
  226. //网络状态服务
  227.             try {  
  228.                 Slog.i(TAG, "NetStat Service");  
  229.                 ServiceManager.addService("netstat"new NetStatService(context));  
  230.             } catch (Throwable e) {  
  231.                 Slog.e(TAG, "Failure starting NetStat Service", e);  
  232.             }  
  233.   
  234. //网络管理服务
  235.             try {  
  236.                 Slog.i(TAG, "NetworkManagement Service");  
  237.                 ServiceManager.addService(  
  238.                         Context.NETWORKMANAGEMENT_SERVICE,  
  239.                         NetworkManagementService.create(context));  
  240.             } catch (Throwable e) {  
  241.                 Slog.e(TAG, "Failure starting NetworkManagement Service", e);  
  242.             }  
  243.  
  244. //网络连接服务 
  245.             try {  
  246.                 Slog.i(TAG, "Connectivity Service");  
  247.                 connectivity = ConnectivityService.getInstance(context);  
  248.                 ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity);  
  249.             } catch (Throwable e) {  
  250.                 Slog.e(TAG, "Failure starting Connectivity Service", e);  
  251.             }  
  252.  
  253. //节流阀控制,提高modem使用效率,避免数据过大或过小情况下不必要的重复连接和断开。 
  254.             try {  
  255.                 Slog.i(TAG, "Throttle Service");  
  256.                 throttle = new ThrottleService(context);  
  257.                 ServiceManager.addService(  
  258.                         Context.THROTTLE_SERVICE, throttle);  
  259.             } catch (Throwable e) {  
  260.                 Slog.e(TAG, "Failure starting ThrottleService", e);  
  261.             }  

  262. //可访问管理服务  
  263.             try {  
  264.               Slog.i(TAG, "Accessibility Manager");  
  265.               ServiceManager.addService(Context.ACCESSIBILITY_SERVICE,  
  266.                       new AccessibilityManagerService(context));  
  267.             } catch (Throwable e) {  
  268.               Slog.e(TAG, "Failure starting Accessibility Manager", e);  
  269.             }  
  270.   
  271. //磁盘加载服务(sd卡等)
  272.             try {  
  273.                 /* 
  274.                  * NotificationManagerService is dependant on MountService, 
  275.                  * (for media / usb notifications) so we must start MountService first. 
  276.                  */  
  277.                 Slog.i(TAG, "Mount Service");  
  278.                 ServiceManager.addService("mount"new MountService(context));  
  279.             } catch (Throwable e) {  
  280.                 Slog.e(TAG, "Failure starting Mount Service", e);  
  281.             }  
  282.   
  283. //通知管理服务
  284.             try {  
  285.                 Slog.i(TAG, "Notification Manager");  
  286.                 notification = new NotificationManagerService(context, statusBar, lights);  
  287.                 ServiceManager.addService(Context.NOTIFICATION_SERVICE, notification);  
  288.             } catch (Throwable e) {  
  289.                 Slog.e(TAG, "Failure starting Notification Manager", e);  
  290.             }  
  291.   
  292. //监控磁盘空间服务
  293.             try {  
  294.                 Slog.i(TAG, "Device Storage Monitor");  
  295.                 ServiceManager.addService(DeviceStorageMonitorService.SERVICE,  
  296.                         new DeviceStorageMonitorService(context));  
  297.             } catch (Throwable e) {  
  298.                 Slog.e(TAG, "Failure starting DeviceStorageMonitor service", e);  
  299.             }  
  300.   
  301. //定位管理服务
  302.             try {  
  303.                 Slog.i(TAG, "Location Manager");  
  304.                 location = new LocationManagerService(context);  
  305.                 ServiceManager.addService(Context.LOCATION_SERVICE, location);  
  306.             } catch (Throwable e) {  
  307.                 Slog.e(TAG, "Failure starting Location Manager", e);  
  308.             }  
  309.  
  310. //搜索管理服务 
  311.             try {  
  312.                 Slog.i(TAG, "Search Service");  
  313.                 ServiceManager.addService(Context.SEARCH_SERVICE,  
  314.                         new SearchManagerService(context));  
  315.             } catch (Throwable e) {  
  316.                 Slog.e(TAG, "Failure starting Search Service", e);  
  317.             }  
  318.  
  319. //开启守护进程 
  320.             if (INCLUDE_DEMO) {  
  321.                 Slog.i(TAG, "Installing demo data...");  
  322.                 (new DemoThread(context)).start();  
  323.             }  
  324.   
  325. //DropBoxManagerService用于生成和管理系统运行时的一些日志文件。这些日志文件大多记录的是系统或某个应用程序出错时的信息。
  326.             try {  
  327.                 Slog.i(TAG, "DropBox Service");  
  328.                 ServiceManager.addService(Context.DROPBOX_SERVICE,  
  329.                         new DropBoxManagerService(context, new File("/data/system/dropbox")));  
  330.             } catch (Throwable e) {  
  331.                 Slog.e(TAG, "Failure starting DropBoxManagerService", e);  
  332.             }  
  333.  
  334. //壁纸管理服务 
  335.             try {  
  336.                 Slog.i(TAG, "Wallpaper Service");  
  337.                 wallpaper = new WallpaperManagerService(context);  
  338.                 ServiceManager.addService(Context.WALLPAPER_SERVICE, wallpaper);  
  339.             } catch (Throwable e) {  
  340.                 Slog.e(TAG, "Failure starting Wallpaper Service", e);  
  341.             }  
  342.  
  343. //音频服务 
  344.             try {  
  345.                 Slog.i(TAG, "Audio Service");  
  346.                 ServiceManager.addService(Context.AUDIO_SERVICE, new AudioService(context));  
  347.             } catch (Throwable e) {  
  348.                 Slog.e(TAG, "Failure starting Audio Service", e);  
  349.             }  

  350. //HeadsetObserver耳机监听  
  351.             try {  
  352.                 Slog.i(TAG, "Headset Observer");  
  353.                 // Listen for wired headset changes  
  354.                 headset = new HeadsetObserver(context);  
  355.             } catch (Throwable e) {  
  356.                 Slog.e(TAG, "Failure starting HeadsetObserver", e);  
  357.             }  

  358. //DockObserver手机亮度的监听  
  359.             try {  
  360.                 Slog.i(TAG, "Dock Observer");  
  361.                 // Listen for dock station changes  
  362.                 dock = new DockObserver(context, power);  
  363.             } catch (Throwable e) {  
  364.                 Slog.e(TAG, "Failure starting DockObserver", e);  
  365.             }  
  366.  
  367. //usb服务 
  368.             try {  
  369.                 Slog.i(TAG, "USB Service");  
  370.                 // Listen for USB changes  
  371.                 usb = new UsbService(context);  
  372.                 ServiceManager.addService(Context.USB_SERVICE, usb);  
  373.             } catch (Throwable e) {  
  374.                 Slog.e(TAG, "Failure starting UsbService", e);  
  375.             }  
  376.  
  377. //ui模式管理服务 
  378.             try {  
  379.                 Slog.i(TAG, "UI Mode Manager Service");  
  380.                 // Listen for UI mode changes  
  381.                 uiMode = new UiModeManagerService(context);  
  382.             } catch (Throwable e) {  
  383.                 Slog.e(TAG, "Failure starting UiModeManagerService", e);  
  384.             }  

  385. //备份管理服务  
  386.             try {  
  387.                 Slog.i(TAG, "Backup Service");  
  388.                 ServiceManager.addService(Context.BACKUP_SERVICE,  
  389.                         new BackupManagerService(context));  
  390.             } catch (Throwable e) {  
  391.                 Slog.e(TAG, "Failure starting Backup Service", e);  
  392.             }  
  393.   
  394. //appWidget服务
  395.             try {  
  396.                 Slog.i(TAG, "AppWidget Service");  
  397.                 appWidget = new AppWidgetService(context);  
  398.                 ServiceManager.addService(Context.APPWIDGET_SERVICE, appWidget);  
  399.             } catch (Throwable e) {  
  400.                 Slog.e(TAG, "Failure starting AppWidget Service", e);  
  401.             }  
  402.   
  403.             try {  
  404.                 Slog.i(TAG, "Recognition Service");  
  405.                 recognition = new RecognitionManagerService(context);  
  406.             } catch (Throwable e) {  
  407.                 Slog.e(TAG, "Failure starting Recognition Service", e);  
  408.             }  
  409.               
  410.             try {  
  411.                 Slog.i(TAG, "DiskStats Service");  
  412.                 ServiceManager.addService("diskstats"new DiskStatsService(context));  
  413.             } catch (Throwable e) {  
  414.                 Slog.e(TAG, "Failure starting DiskStats Service", e);  
  415.             }  
  416.         }  
  417.   
  418.         // make sure the ADB_ENABLED setting value matches the secure property value  
  419.         Settings.Secure.putInt(mContentResolver, Settings.Secure.ADB_ENABLED,  
  420.                 "1".equals(SystemProperties.get("persist.service.adb.enable")) ? 1 : 0);  
  421.   
  422.         // register observer to listen for settings changes  
  423.         mContentResolver.registerContentObserver(Settings.Secure.getUriFor(Settings.Secure.ADB_ENABLED),  
  424.                 falsenew AdbSettingsObserver());  
  425.   
  426.         // Before things start rolling, be sure we have decided whether  
  427.         // we are in safe mode.  
  428.         final boolean safeMode = wm.detectSafeMode();  
  429.         if (safeMode) {  
  430.             try {  
  431.                 ActivityManagerNative.getDefault().enterSafeMode();  
  432.                 // Post the safe mode state in the Zygote class  
  433.                 Zygote.systemInSafeMode = true;  
  434.                 // Disable the JIT for the system_server process  
  435.                 VMRuntime.getRuntime().disableJitCompilation();  
  436.             } catch (RemoteException e) {  
  437.             }  
  438.         } else {  
  439.             // Enable the JIT for the system_server process  
  440.             VMRuntime.getRuntime().startJitCompilation();  
  441.         }  
  442.   
  443.         // It is now time to start up the app processes...  
  444.   
  445.         if (devicePolicy != null) {  
  446.             devicePolicy.systemReady();  
  447.         }  
  448.   
  449.         if (notification != null) {  
  450.             notification.systemReady();  
  451.         }  
  452.   
  453.         if (statusBar != null) {  
  454.             statusBar.systemReady();  
  455.         }  
  456.         wm.systemReady();  
  457.         power.systemReady();  
  458.         try {  
  459.             pm.systemReady();  
  460.         } catch (RemoteException e) {  
  461.         }  
  462.   
  463.         // These are needed to propagate to the runnable below.  
  464.         final StatusBarManagerService statusBarF = statusBar;  
  465.         final BatteryService batteryF = battery;  
  466.         final ConnectivityService connectivityF = connectivity;  
  467.         final DockObserver dockF = dock;  
  468.         final UsbService usbF = usb;  
  469.         final ThrottleService throttleF = throttle;  
  470.         final UiModeManagerService uiModeF = uiMode;  
  471.         final AppWidgetService appWidgetF = appWidget;  
  472.         final WallpaperManagerService wallpaperF = wallpaper;  
  473.         final InputMethodManagerService immF = imm;  
  474.         final RecognitionManagerService recognitionF = recognition;  
  475.         final LocationManagerService locationF = location;  
  476.   
  477.         // We now tell the activity manager it is okay to run third party  
  478.         // code.  It will call back into us once it has gotten to the state  
  479.         // where third party code can really run (but before it has actually  
  480.         // started launching the initial applications), for us to complete our  
  481.         // initialization.  
  482.         ((ActivityManagerService)ActivityManagerNative.getDefault())  
  483.                 .systemReady(new Runnable() {  
  484.             public void run() {  
  485.                 Slog.i(TAG, "Making services ready");  
  486.   
  487.                 if (statusBarF != null) statusBarF.systemReady2();  
  488.                 if (batteryF != null) batteryF.systemReady();  
  489.                 if (connectivityF != null) connectivityF.systemReady();  
  490.                 if (dockF != null) dockF.systemReady();  
  491.                 if (usbF != null) usbF.systemReady();  
  492.                 if (uiModeF != null) uiModeF.systemReady();  
  493.                 if (recognitionF != null) recognitionF.systemReady();  
  494.                 Watchdog.getInstance().start();  
  495.   
  496.                 // It is now okay to let the various system services start their  
  497.                 // third party code...  
  498.   
  499.                 if (appWidgetF != null) appWidgetF.systemReady(safeMode);  
  500.                 if (wallpaperF != null) wallpaperF.systemReady();  
  501.                 if (immF != null) immF.systemReady();  
  502.                 if (locationF != null) locationF.systemReady();  
  503.                 if (throttleF != null) throttleF.systemReady();  
  504.             }  
  505.         });  
  506.   
  507.         // For debug builds, log event loop stalls to dropbox for analysis.  
  508.         if (StrictMode.conditionallyEnableDebugLogging()) {  
  509.             Slog.i(TAG, "Enabled StrictMode for system server main thread.");  
  510.         }  
  511.   
  512.         Looper.loop();  
  513.         Slog.d(TAG, "System ServerThread is exiting!");  
  514.     }  
  515. }  

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









抱歉!评论已关闭.