[转]Android情景分析之深入解析system_server 2

来源:互联网 发布:mac 网页抓取工具 编辑:程序博客网 时间:2024/05/22 07:54

转自:http://blog.csdn.net/hu3167343/article/details/38375167

system_server的真面目

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static void main(String[] args) {  
  2.         ……  
  3.         System.loadLibrary("android_servers");  
  4.         Slog.i(TAG, "Entered the Android system server!");  
  5.   
  6.         // Initialize native services.  
  7.         nativeInit();  
  8.   
  9.         // This used to be its own separate thread, but now it is  
  10.         // just the loop we run on the main thread.  
  11.         ServerThread thr = new ServerThread();  
  12.         thr.initAndLoop();  
  13. }  

函数首先加载了libandroid_servers.so文件,之后调用nativeInit初始化,最后initAndLoop启动各种系统服务。


Native服务初始化 - nativeInit

nativeInit是一个native函数,代码在frameworks\base\services\jni\com_android_server_SystemServer.cpp中:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. static void android_server_SystemServer_nativeInit(JNIEnv* env, jobject clazz) {  
  2.     char propBuf[PROPERTY_VALUE_MAX];  
  3.     property_get("system_init.startsensorservice", propBuf, "1");  
  4.     if (strcmp(propBuf, "1") == 0) {  
  5.         // Start the sensor service  
  6.         SensorService::instantiate();  
  7.     }  
  8. }  

这个函数主要是根据属性配置文件来确定是否需要初始化Sensor Service,这里就不多介绍了。


Java层服务初始化 – ServerThread类

启动一个ServerThread线程,其主要用来初始化Java层的各种服务,并且通过Binder接收各种服务消息。我们直接来看initAndLoop函数。这个函数比较长,大致看看它都干了些什么:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public void initAndLoop() {  
  2.         // 初始化Lopper  
  3.         Looper.prepareMainLooper();  
  4.       
  5.         // 设置线程优先级  
  6.         android.os.Process.setThreadPriority(  
  7.                 android.os.Process.THREAD_PRIORITY_FOREGROUND);  
  8.   
  9.         BinderInternal.disableBackgroundScheduling(true);  
  10.         android.os.Process.setCanSelfBackground(false);  
  11.   
  12.         ......  
  13.   
  14.         // bootstrap services  
  15.         boolean onlyCore = false;  
  16.         boolean firstBoot = false;  
  17.         try {  
  18.             .....  
  19.   
  20.             Slog.i(TAG, "Power Manager");  
  21.             power = new PowerManagerService();  
  22.             ServiceManager.addService(Context.POWER_SERVICE, power);  
  23.   
  24.             Slog.i(TAG, "Activity Manager");  
  25.             context = ActivityManagerService.main(factoryTest);  
  26.         } catch (RuntimeException e) {  
  27.             Slog.e("System""******************************************");  
  28.             Slog.e("System""************ Failure starting bootstrap service", e);  
  29.         }  
  30.   
  31.         boolean disableStorage = SystemProperties.getBoolean("config.disable_storage"false);  
  32.         boolean disableMedia = SystemProperties.getBoolean("config.disable_media"false);  
  33.         boolean disableBluetooth = SystemProperties.getBoolean("config.disable_bluetooth"false);  
  34.         boolean disableTelephony = SystemProperties.getBoolean("config.disable_telephony"false);  
  35.         boolean disableLocation = SystemProperties.getBoolean("config.disable_location"false);  
  36.         boolean disableSystemUI = SystemProperties.getBoolean("config.disable_systemui"false);  
  37.         boolean disableNonCoreServices = SystemProperties.getBoolean("config.disable_noncore"false);  
  38.         boolean disableNetwork = SystemProperties.getBoolean("config.disable_network"false);  
  39.   
  40.         try {  
  41.             Slog.i(TAG, "Display Manager");  
  42.             display = new DisplayManagerService(context, wmHandler);  
  43.             ServiceManager.addService(Context.DISPLAY_SERVICE, display, true);  
  44.   
  45.             Slog.i(TAG, "Telephony Registry");  
  46.             telephonyRegistry = new TelephonyRegistry(context);  
  47.             ServiceManager.addService("telephony.registry", telephonyRegistry);  
  48.   
  49.             Slog.i(TAG, "Scheduling Policy");  
  50.             ServiceManager.addService("scheduling_policy"new SchedulingPolicyService());  
  51.   
  52.             AttributeCache.init(context);  
  53.   
  54.             if (!display.waitForDefaultDisplay()) {  
  55.                 reportWtf("Timeout waiting for default display to be initialized.",  
  56.                         new Throwable());  
  57.             }  
  58.   
  59.             Slog.i(TAG, "Package Manager");  
  60.             // Only run "core" apps if we're encrypting the device.  
  61.             String cryptState = SystemProperties.get("vold.decrypt");  
  62.             if (ENCRYPTING_STATE.equals(cryptState)) {  
  63.                 Slog.w(TAG, "Detected encryption in progress - only parsing core apps");  
  64.                 onlyCore = true;  
  65.             } else if (ENCRYPTED_STATE.equals(cryptState)) {  
  66.                 Slog.w(TAG, "Device encrypted - only parsing core apps");  
  67.                 onlyCore = true;  
  68.             }  
  69.   
  70.             pm = PackageManagerService.main(context, installer,  
  71.                     factoryTest != SystemServer.FACTORY_TEST_OFF,  
  72.                     onlyCore);  
  73.             try {  
  74.                 firstBoot = pm.isFirstBoot();  
  75.             } catch (RemoteException e) {  
  76.             }  
  77.   
  78.             ActivityManagerService.setSystemProcess();  
  79.   
  80.             Slog.i(TAG, "Entropy Mixer");  
  81.             ServiceManager.addService("entropy"new EntropyMixer(context));  
  82.   
  83.             Slog.i(TAG, "User Service");  
  84.             ServiceManager.addService(Context.USER_SERVICE,  
  85.                     UserManagerService.getInstance());  
  86.   
  87.             mContentResolver = context.getContentResolver();  
  88.   
  89.             // The AccountManager must come before the ContentService  
  90.             try {  
  91.                 // TODO: seems like this should be disable-able, but req'd by ContentService  
  92.                 Slog.i(TAG, "Account Manager");  
  93.                 accountManager = new AccountManagerService(context);  
  94.                 ServiceManager.addService(Context.ACCOUNT_SERVICE, accountManager);  
  95.             } catch (Throwable e) {  
  96.                 Slog.e(TAG, "Failure starting Account Manager", e);  
  97.             }  
  98.   
  99.             Slog.i(TAG, "Content Manager");  
  100.             contentService = ContentService.main(context,  
  101.                     factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL);  
  102.   
  103.             Slog.i(TAG, "System Content Providers");  
  104.             ActivityManagerService.installSystemProviders();  
  105.   
  106.             Slog.i(TAG, "Lights Service");  
  107.             lights = new LightsService(context);  
  108.   
  109.             Slog.i(TAG, "Battery Service");  
  110.             battery = new BatteryService(context, lights);  
  111.             ServiceManager.addService("battery", battery);  
  112.   
  113.             Slog.i(TAG, "Vibrator Service");  
  114.             vibrator = new VibratorService(context);  
  115.             ServiceManager.addService("vibrator", vibrator);  
  116.   
  117.             Slog.i(TAG, "Consumer IR Service");  
  118.             consumerIr = new ConsumerIrService(context);  
  119.             ServiceManager.addService(Context.CONSUMER_IR_SERVICE, consumerIr);  
  120.   
  121.             // only initialize the power service after we have started the  
  122.             // lights service, content providers and the battery service.  
  123.             power.init(context, lights, ActivityManagerService.self(), battery,  
  124.                     BatteryStatsService.getService(),  
  125.                     ActivityManagerService.self().getAppOpsService(), display);  
  126.   
  127.             Slog.i(TAG, "Alarm Manager");  
  128.             alarm = new AlarmManagerService(context);  
  129.             ServiceManager.addService(Context.ALARM_SERVICE, alarm);  
  130.   
  131.             Slog.i(TAG, "Init Watchdog");  
  132.             Watchdog.getInstance().init(context, battery, power, alarm,  
  133.                     ActivityManagerService.self());  
  134.             Watchdog.getInstance().addThread(wmHandler, "WindowManager thread");  
  135.   
  136.             Slog.i(TAG, "Input Manager");  
  137.             inputManager = new InputManagerService(context, wmHandler);  
  138.   
  139.             Slog.i(TAG, "Window Manager");  
  140.             wm = WindowManagerService.main(context, power, display, inputManager,  
  141.                     wmHandler, factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL,  
  142.                     !firstBoot, onlyCore);  
  143.             ServiceManager.addService(Context.WINDOW_SERVICE, wm);  
  144.             ServiceManager.addService(Context.INPUT_SERVICE, inputManager);  
  145.   
  146.             ActivityManagerService.self().setWindowManager(wm);  
  147.   
  148.             inputManager.setWindowManagerCallbacks(wm.getInputMonitor());  
  149.             inputManager.start();  
  150.   
  151.             display.setWindowManager(wm);  
  152.             display.setInputManager(inputManager);  
  153.     }  
  154.   
  155.     ......  
  156.   
  157.         Looper.loop();  
  158.         Slog.d(TAG, "System ServerThread is exiting!");  
  159. }  

以上代码省略了很多,大致就是ServerThread线程启动各种系统服务,最后通过Looper.loop()来进行消息循环,然后处理消息。


system_server总结