Android的系统服务System Server,包含众多内置的服务,这些服务通过Android提供的binder 通信机制注册在Service Manager中,最终是写到内核空间中去。在Android上层通过binder机制可以直接从用户空间到内核空间访问这些服务,这就是Android运行的基石。本文是想将Android的服务总结一下,便于我们接下来各个击破,分析系统服务的架构。
SystemServer.java
private void run() {
try {
//......
// The system server should never make non-oneway calls
Binder.setWarnOnBlocking(true);
//......
// Ensure binder calls into the system always run at foreground priority.
BinderInternal.disableBackgroundScheduling(true);
// Increase the number of binder threads in system_server
BinderInternal.setMaxThreads(sMaxBinderThreads);
// Prepare the main looper thread (this thread).
android.os.Process.setThreadPriority(
android.os.Process.THREAD_PRIORITY_FOREGROUND);
android.os.Process.setCanSelfBackground(false);
Looper.prepareMainLooper();
// Initialize native services.
System.loadLibrary("android_servers");
//......
createSystemContext();
//step1:
// Create the system service manager.
mSystemServiceManager = new SystemServiceManager(mSystemContext);
mSystemServiceManager.setRuntimeRestarted(mRuntimeRestart);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
//......
} finally {
traceEnd(); // InitBeforeStartServices
}
// Start services.
try {
traceBeginAndSlog("StartServices");
//step2:
startBootstrapServices();
startCoreServices();
startOtherServices();
SystemServerInitThreadPool.shutdown();
} catch (Throwable ex) {
Slog.e("System", "******************************************");
Slog.e("System", "************ Failure starting system services", ex);
throw ex;
} finally {
traceEnd();
}
//......
// Loop forever.
Looper.loop();
throw new RuntimeException("Main thread loop unexpectedly exited");
}
step1:构造了SystemServiceManager对象,SystemServiceManager主要是管理Android系统中众多的Service,其中提供了反射构造众多系统服务。
step2:这儿执行了三个函数:
1.startBootstrapServices()
这里的bootstrap代表Android系统的引导程序,这是系统中最核心的部分。这些系统服务之间的依赖性比较强。需要在一起统一管理。
2.startCoreService()
核心服务想对引导服务优先级低一些,主要是启动一些电池管理服务、process usage状态管理服务、LED服务等等。
3.startOtherService()
这个优先级就更低了,主要是vibratorService、Network管理服务、Audio服务等等。
虽然这三类服务的启动时机有先后关系,但是实际上它们都是系统开始的时候启动的,有了这些服务的通力配合,Android系统才能更好地运行。
下面列出了Android中所有注册的系统服务(有些系统服务是在条件成立的情况下注册的,例如Vr服务),但是不可能都讲,我会挑一些重点的系统服务,绝对讲透,从调用流程到调用逻辑,再到调用原理。下面列出一些计划:
1.ActivityManagerService
2.WindowManagerService
3.PackageManagerService
4.InputManagerService
5.JobSchedulerService
6.PowerManagerService

网友评论