Android(安卓)之 SystemService
16lz
2021-12-04
转自: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
- nativepublicstaticvoidinit1(String[]args);
- publicstaticvoidmain(String[]args){
- if(SamplingProfilerIntegration.isEnabled()){
- SamplingProfilerIntegration.start();
- timer=newTimer();
- timer.schedule(newTimerTask(){
- @Override
- publicvoidrun(){
- SamplingProfilerIntegration.writeSnapshot("system_server");
- }
- },SNAPSHOT_INTERVAL,SNAPSHOT_INTERVAL);
- }
- //Thesystemserverhastorunallofthetime,soitneedstobe
- //asefficientaspossiblewithitsmemoryusage.
- VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
- System.loadLibrary("android_servers");//加载本地库android_servers
- init1(args);
- }
在main函数中主要是调用了本地方法init1(args), 他的实现位于../base/services/jni/com_android_server_SystemService.cpp中
[java] view plain copy- staticvoidandroid_server_SystemServer_init1(JNIEnv*env,jobjectclazz)
- {
- system_init();
- }
进一步来看system_init,在这里面看到了闭合循环管理框架:
- runtime->callStatic("com/android/server/SystemServer","init2");//回调了SystemServer.java中的init2方法
- if(proc->supportsProcesses()){
- LOGI("Systemserver:enteringthreadpool.\n");
- ProcessState::self()->startThreadPool();
- IPCThreadState::self()->joinThreadPool();
- LOGI("Systemserver:exitingthreadpool.\n");
- }
通过调用com/android/server/SystemServer.java中的init2方法完成service的注册。在init2方法中主要建立了以ServerThread线程,然后启动线程来完成service的注册。
[java] view plain copy- publicstaticfinalvoidinit2(){
- Slog.i(TAG,"EnteredtheAndroidsystemserver!");
- Threadthr=newServerThread();
- thr.setName("android.server.ServerThread");
- thr.start();
- }
具体实现service的注册在ServerThread的run方法中:
[java] view plain copy- try{
- Slog.i(TAG,"EntropyService");
- ServiceManager.addService("entropy",newEntropyService());
- Slog.i(TAG,"PowerManager");
- power=newPowerManagerService();
- ServiceManager.addService(Context.POWER_SERVICE,power);
- Slog.i(TAG,"ActivityManager");
- context=ActivityManagerService.main(factoryTest);
- Slog.i(TAG,"TelephonyRegistry");
- ServiceManager.addService("telephony.registry",newTelephonyRegistry(context));
- }
更多相关文章
- android progressDialog的使用
- 详解Android中的Activity生命周期
- Android中设置定时器
- 面试例题6:两种方法将图像显示在View上
- Android之Handler用法总结
- Android(安卓)NDK报错(Eclipse)及解决方法
- 【Android】16.5 Android内置的系统服务
- Android(安卓)更新UI的两个方法
- 【Android(安卓)基础】Android中全屏或者取消标题栏