转自:http://blog.csdn.net/hu3167343/article/details/38375167
system_server的真面目
- public static void main(String[] args) {
- ……
- System.loadLibrary("android_servers");
- Slog.i(TAG, "Entered the Android system server!");
- // Initialize native services.
- nativeInit();
- // This used to be its own separate thread, but now it is
- // just the loop we run on the main thread.
- ServerThread thr = new ServerThread();
- thr.initAndLoop();
- }
函数首先加载了libandroid_servers.so文件,之后调用nativeInit初始化,最后initAndLoop启动各种系统服务。
Native服务初始化 - nativeInit
nativeInit是一个native函数,代码在frameworks\base\services\jni\com_android_server_SystemServer.cpp中:
- static void android_server_SystemServer_nativeInit(JNIEnv* env, jobject clazz) {
- char propBuf[PROPERTY_VALUE_MAX];
- property_get("system_init.startsensorservice", propBuf, "1");
- if (strcmp(propBuf, "1") == 0) {
- // Start the sensor service
- SensorService::instantiate();
- }
- }
这个函数主要是根据属性配置文件来确定是否需要初始化Sensor Service,这里就不多介绍了。
Java层服务初始化 – ServerThread类
启动一个ServerThread线程,其主要用来初始化Java层的各种服务,并且通过Binder接收各种服务消息。我们直接来看initAndLoop函数。这个函数比较长,大致看看它都干了些什么:
- public void initAndLoop() {
- // 初始化Lopper
- Looper.prepareMainLooper();
- // 设置线程优先级
- android.os.Process.setThreadPriority(
- android.os.Process.THREAD_PRIORITY_FOREGROUND);
- BinderInternal.disableBackgroundScheduling(true);
- android.os.Process.setCanSelfBackground(false);
- ......
- // bootstrap services
- boolean onlyCore = false;
- boolean firstBoot = false;
- try {
- .....
- Slog.i(TAG, "Power Manager");
- power = new PowerManagerService();
- ServiceManager.addService(Context.POWER_SERVICE, power);
- Slog.i(TAG, "Activity Manager");
- context = ActivityManagerService.main(factoryTest);
- } catch (RuntimeException e) {
- Slog.e("System", "******************************************");
- Slog.e("System", "************ Failure starting bootstrap service", e);
- }
- boolean disableStorage = SystemProperties.getBoolean("config.disable_storage", false);
- boolean disableMedia = SystemProperties.getBoolean("config.disable_media", false);
- boolean disableBluetooth = SystemProperties.getBoolean("config.disable_bluetooth", false);
- boolean disableTelephony = SystemProperties.getBoolean("config.disable_telephony", false);
- boolean disableLocation = SystemProperties.getBoolean("config.disable_location", false);
- boolean disableSystemUI = SystemProperties.getBoolean("config.disable_systemui", false);
- boolean disableNonCoreServices = SystemProperties.getBoolean("config.disable_noncore", false);
- boolean disableNetwork = SystemProperties.getBoolean("config.disable_network", false);
- try {
- Slog.i(TAG, "Display Manager");
- display = new DisplayManagerService(context, wmHandler);
- ServiceManager.addService(Context.DISPLAY_SERVICE, display, true);
- Slog.i(TAG, "Telephony Registry");
- telephonyRegistry = new TelephonyRegistry(context);
- ServiceManager.addService("telephony.registry", telephonyRegistry);
- Slog.i(TAG, "Scheduling Policy");
- ServiceManager.addService("scheduling_policy", new SchedulingPolicyService());
- AttributeCache.init(context);
- if (!display.waitForDefaultDisplay()) {
- reportWtf("Timeout waiting for default display to be initialized.",
- new Throwable());
- }
- Slog.i(TAG, "Package Manager");
- // Only run "core" apps if we're encrypting the device.
- String cryptState = SystemProperties.get("vold.decrypt");
- if (ENCRYPTING_STATE.equals(cryptState)) {
- Slog.w(TAG, "Detected encryption in progress - only parsing core apps");
- onlyCore = true;
- } else if (ENCRYPTED_STATE.equals(cryptState)) {
- Slog.w(TAG, "Device encrypted - only parsing core apps");
- onlyCore = true;
- }
- pm = PackageManagerService.main(context, installer,
- factoryTest != SystemServer.FACTORY_TEST_OFF,
- onlyCore);
- try {
- firstBoot = pm.isFirstBoot();
- } catch (RemoteException e) {
- }
- ActivityManagerService.setSystemProcess();
- Slog.i(TAG, "Entropy Mixer");
- ServiceManager.addService("entropy", new EntropyMixer(context));
- Slog.i(TAG, "User Service");
- ServiceManager.addService(Context.USER_SERVICE,
- UserManagerService.getInstance());
- mContentResolver = context.getContentResolver();
- // The AccountManager must come before the ContentService
- try {
- // TODO: seems like this should be disable-able, but req'd by ContentService
- Slog.i(TAG, "Account Manager");
- accountManager = new AccountManagerService(context);
- ServiceManager.addService(Context.ACCOUNT_SERVICE, accountManager);
- } catch (Throwable e) {
- Slog.e(TAG, "Failure starting Account Manager", e);
- }
- Slog.i(TAG, "Content Manager");
- contentService = ContentService.main(context,
- factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL);
- Slog.i(TAG, "System Content Providers");
- ActivityManagerService.installSystemProviders();
- Slog.i(TAG, "Lights Service");
- lights = new LightsService(context);
- Slog.i(TAG, "Battery Service");
- battery = new BatteryService(context, lights);
- ServiceManager.addService("battery", battery);
- Slog.i(TAG, "Vibrator Service");
- vibrator = new VibratorService(context);
- ServiceManager.addService("vibrator", vibrator);
- Slog.i(TAG, "Consumer IR Service");
- consumerIr = new ConsumerIrService(context);
- ServiceManager.addService(Context.CONSUMER_IR_SERVICE, consumerIr);
- // only initialize the power service after we have started the
- // lights service, content providers and the battery service.
- power.init(context, lights, ActivityManagerService.self(), battery,
- BatteryStatsService.getService(),
- ActivityManagerService.self().getAppOpsService(), display);
- Slog.i(TAG, "Alarm Manager");
- alarm = new AlarmManagerService(context);
- ServiceManager.addService(Context.ALARM_SERVICE, alarm);
- Slog.i(TAG, "Init Watchdog");
- Watchdog.getInstance().init(context, battery, power, alarm,
- ActivityManagerService.self());
- Watchdog.getInstance().addThread(wmHandler, "WindowManager thread");
- Slog.i(TAG, "Input Manager");
- inputManager = new InputManagerService(context, wmHandler);
- Slog.i(TAG, "Window Manager");
- wm = WindowManagerService.main(context, power, display, inputManager,
- wmHandler, factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL,
- !firstBoot, onlyCore);
- ServiceManager.addService(Context.WINDOW_SERVICE, wm);
- ServiceManager.addService(Context.INPUT_SERVICE, inputManager);
- ActivityManagerService.self().setWindowManager(wm);
- inputManager.setWindowManagerCallbacks(wm.getInputMonitor());
- inputManager.start();
- display.setWindowManager(wm);
- display.setInputManager(inputManager);
- }
- ......
- Looper.loop();
- Slog.d(TAG, "System ServerThread is exiting!");
- }
以上代码省略了很多,大致就是ServerThread线程启动各种系统服务,最后通过Looper.loop()来进行消息循环,然后处理消息。