Android入门进阶教程(12)-SystemService详解
2013-06-01 18:23
573 查看
SystemServer是Android系统的一个核心进程,它是由zygote进程创建的,因此在android的启动过程中位于zygote之后。android的所有服务循环都是建立在 SystemServer之上的。在SystemServer中,将可以看到它建立了android中的大部分服务,并通过ServerManager的add_service方法把这些服务加入到了ServiceManager的svclist中。从而完成ServcieManager对服务的管理。
先看下SystemServer的main函数:
[java] view
plaincopy
native public static void init1(String[]args);
public static void main(String[] args) {
if(SamplingProfilerIntegration.isEnabled()) {
SamplingProfilerIntegration.start();
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
SamplingProfilerIntegration.writeSnapshot("system_server");
}
}, SNAPSHOT_INTERVAL,SNAPSHOT_INTERVAL);
}
// The system server has to run all ofthe time, so it needs to be
// as efficient as possible with itsmemory usage.
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
plaincopy
static voidandroid_server_SystemServer_init1(JNIEnv* env, jobject clazz)
{
system_init();
}
进一步来看system_init,在这里面看到了闭合循环管理框架:
[java] view
plaincopy
runtime->callStatic("com/android/server/SystemServer","init2");//回调了SystemServer.java中的init2方法
if (proc->supportsProcesses()) {
LOGI("System server: enteringthread pool.\n");
ProcessState::self()->startThreadPool();
IPCThreadState::self()->joinThreadPool();
LOGI("System server: exitingthread pool.\n");
}
通过调用com/android/server/SystemServer.java中的init2方法完成service的注册。在init2方法中主要建立了以ServerThread线程,然后启动线程来完成service的注册。
[java] view
plaincopy
public static final void init2() {
Slog.i(TAG, "Entered the Androidsystem server!");
Thread thr = new ServerThread();
thr.setName("android.server.ServerThread");
thr.start();
}
具体实现service的注册在ServerThread的run方法中:
[java] view
plaincopy
try {
Slog.i(TAG, "EntropyService");
ServiceManager.addService("entropy", new EntropyService());
Slog.i(TAG, "PowerManager");
power = new PowerManagerService();
ServiceManager.addService(Context.POWER_SERVICE, power);
Slog.i(TAG, "ActivityManager");
context =ActivityManagerService.main(factoryTest);
Slog.i(TAG, "TelephonyRegistry");
ServiceManager.addService("telephony.registry", newTelephonyRegistry(context));
}
先看下SystemServer的main函数:
[java] view
plaincopy
native public static void init1(String[]args);
public static void main(String[] args) {
if(SamplingProfilerIntegration.isEnabled()) {
SamplingProfilerIntegration.start();
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
SamplingProfilerIntegration.writeSnapshot("system_server");
}
}, SNAPSHOT_INTERVAL,SNAPSHOT_INTERVAL);
}
// The system server has to run all ofthe time, so it needs to be
// as efficient as possible with itsmemory usage.
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
plaincopy
static voidandroid_server_SystemServer_init1(JNIEnv* env, jobject clazz)
{
system_init();
}
进一步来看system_init,在这里面看到了闭合循环管理框架:
[java] view
plaincopy
runtime->callStatic("com/android/server/SystemServer","init2");//回调了SystemServer.java中的init2方法
if (proc->supportsProcesses()) {
LOGI("System server: enteringthread pool.\n");
ProcessState::self()->startThreadPool();
IPCThreadState::self()->joinThreadPool();
LOGI("System server: exitingthread pool.\n");
}
通过调用com/android/server/SystemServer.java中的init2方法完成service的注册。在init2方法中主要建立了以ServerThread线程,然后启动线程来完成service的注册。
[java] view
plaincopy
public static final void init2() {
Slog.i(TAG, "Entered the Androidsystem server!");
Thread thr = new ServerThread();
thr.setName("android.server.ServerThread");
thr.start();
}
具体实现service的注册在ServerThread的run方法中:
[java] view
plaincopy
try {
Slog.i(TAG, "EntropyService");
ServiceManager.addService("entropy", new EntropyService());
Slog.i(TAG, "PowerManager");
power = new PowerManagerService();
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入门进阶教程(8)-常用组件 打电话,发短信简单应用 &单元测试
- Android入门进阶教程(9)-输入输出流
- Android入门进阶教程(10)-Xml解析
- Android基础入门教程——4.2.2 Service进阶
- Android入门进阶教程(1)-android扫盲
- Android入门进阶教程(2)-windows 开发环境的搭建
- Android入门进阶教程(11)-远程图片获取和Xml解析
- Android入门进阶教程(3)-linux 开发环境的搭建
- Android入门进阶教程(13)-ServiceManager服务管理详解
- Android入门进阶教程(4)-android hello world开发
- Android入门进阶教程(14)-Binder进程通信介绍
- Android入门进阶教程(1)-android扫盲
- Android入门进阶教程(15)-进程创建zygote 详解
- Android入门进阶教程(20)-ContentProvider 数据共享
- Android入门进阶教程(2)-windows 开发环境的搭建
- Android入门进阶教程(21)-通知管理器NotificationManager
- Android入门进阶教程(3)-linux 开发环境的搭建
- Android入门进阶教程(5)-android 常用组件
- Android入门进阶教程(16)-ActivityThead、ActivityManagerService 详解
- Android入门进阶教程(4)-android hello world开发