分享

Android 之 SystemService

 老匹夫 2013-12-25
分类: Android 2011-07-13 13:48 3161人阅读 评论(0) 收藏 举报

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

先看下SystemServer的main函数:

  1. native public static void init1(String[]args);  
  2. public static void main(String[] args) {  
  3.        if(SamplingProfilerIntegration.isEnabled()) {  
  4.           SamplingProfilerIntegration.start();  
  5.            timer = new Timer();  
  6.            timer.schedule(new TimerTask() {  
  7.                @Override  
  8.                public void run() {  
  9.                   SamplingProfilerIntegration.writeSnapshot("system_server");  
  10.                }  
  11.            }, SNAPSHOT_INTERVAL,SNAPSHOT_INTERVAL);  
  12.        }  
  13.        // The system server has to run all ofthe time, so it needs to be   
  14.        // as efficient as possible with itsmemory usage.   
  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中

  1. static voidandroid_server_SystemServer_init1(JNIEnv* env, jobject clazz)  
  2. {  
  3.     system_init();  
  4. }  

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

  1. runtime->callStatic("com/android/server/SystemServer","init2");//回调了SystemServer.java中的init2方法   
  2.     if (proc->supportsProcesses()) {  
  3.         LOGI("System server: enteringthread pool.\n");  
  4.        ProcessState::self()->startThreadPool();  
  5.        IPCThreadState::self()->joinThreadPool();  
  6.         LOGI("System server: exitingthread pool.\n");  
  7.     }  

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

  1. public static final void init2() {  
  2.     Slog.i(TAG, "Entered the Androidsystem server!");  
  3.     Thread thr = new ServerThread();  
  4.    thr.setName("android.server.ServerThread");  
  5.     thr.start();  
  6. }  

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

  1.  try {  
  2.             Slog.i(TAG, "EntropyService");  
  3.            ServiceManager.addService("entropy"new EntropyService());  
  4.             Slog.i(TAG, "PowerManager");  
  5.             power = new PowerManagerService();  
  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.   
  12. }  


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多