转自:http://blog.csdn.net/xieqibao/article/details/6602820

SystemServer是Android系统的一个核心进程,它是由zygote进程创建的,因此在android的启动过程中位于zygote之后。android的所有服务循环都是建立在 SystemServer之上的。在SystemServer中,将可以看到它建立了android中的大部分服务,并通过ServerManager的add_service方法把这些服务加入到了ServiceManager的svclist中。从而完成ServcieManager对服务的管理。

先看下SystemServer的main函数:

[java] view plain copy
  1. nativepublicstaticvoidinit1(String[]args);
  2. publicstaticvoidmain(String[]args){
  3. if(SamplingProfilerIntegration.isEnabled()){
  4. SamplingProfilerIntegration.start();
  5. timer=newTimer();
  6. timer.schedule(newTimerTask(){
  7. @Override
  8. publicvoidrun(){
  9. SamplingProfilerIntegration.writeSnapshot("system_server");
  10. }
  11. },SNAPSHOT_INTERVAL,SNAPSHOT_INTERVAL);
  12. }
  13. //Thesystemserverhastorunallofthetime,soitneedstobe
  14. //asefficientaspossiblewithitsmemoryusage.
  15. VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
  16. System.loadLibrary("android_servers");//加载本地库android_servers
  17. init1(args);
  18. }

在main函数中主要是调用了本地方法init1(args), 他的实现位于../base/services/jni/com_android_server_SystemService.cpp中

[java] view plain copy
  1. staticvoidandroid_server_SystemServer_init1(JNIEnv*env,jobjectclazz)
  2. {
  3. system_init();
  4. }

进一步来看system_init,在这里面看到了闭合循环管理框架:

[java] view plain copy
  1. runtime->callStatic("com/android/server/SystemServer","init2");//回调了SystemServer.java中的init2方法
  2. if(proc->supportsProcesses()){
  3. LOGI("Systemserver:enteringthreadpool.\n");
  4. ProcessState::self()->startThreadPool();
  5. IPCThreadState::self()->joinThreadPool();
  6. LOGI("Systemserver:exitingthreadpool.\n");
  7. }

通过调用com/android/server/SystemServer.java中的init2方法完成service的注册。在init2方法中主要建立了以ServerThread线程,然后启动线程来完成service的注册。

[java] view plain copy
  1. publicstaticfinalvoidinit2(){
  2. Slog.i(TAG,"EnteredtheAndroidsystemserver!");
  3. Threadthr=newServerThread();
  4. thr.setName("android.server.ServerThread");
  5. thr.start();
  6. }

具体实现service的注册在ServerThread的run方法中:

[java] view plain copy
  1. try{
  2. Slog.i(TAG,"EntropyService");
  3. ServiceManager.addService("entropy",newEntropyService());
  4. Slog.i(TAG,"PowerManager");
  5. power=newPowerManagerService();
  6. ServiceManager.addService(Context.POWER_SERVICE,power);
  7. Slog.i(TAG,"ActivityManager");
  8. context=ActivityManagerService.main(factoryTest);
  9. Slog.i(TAG,"TelephonyRegistry");
  10. ServiceManager.addService("telephony.registry",newTelephonyRegistry(context));
  11. }


更多相关文章

  1. android progressDialog的使用
  2. 详解Android中的Activity生命周期
  3. Android中设置定时器
  4. 面试例题6:两种方法将图像显示在View上
  5. Android之Handler用法总结
  6. Android(安卓)NDK报错(Eclipse)及解决方法
  7. 【Android】16.5 Android内置的系统服务
  8. Android(安卓)更新UI的两个方法
  9. 【Android(安卓)基础】Android中全屏或者取消标题栏

随机推荐

  1. 手机安全卫士开发系列(5)——自动更新
  2. setonClickListener()的解释
  3. Android(安卓)Lesson Two: Ambient and D
  4. Android(安卓)studio 安卓填写注册表单源
  5. Android(安卓)开启关闭软键盘
  6. android layout动态布局
  7. Log控制输出
  8. java.lang.NullPointerException: Attemp
  9. android游戏开发框架libgdx的使用—使用T
  10. the android development environment