SystemServer在ZygoteInit中进行创建,并且启动起来的。
/frameworks/base/core/java/com/android/internal/os/ZygoteInit.java
前面我们讲过android系统中比较重要的几个进程:init进程,Zygote进程,SystemServer进程已经各种应用进程,其中Zygote进程是整个android系统的根进程,包含SystemServer进程已经各种应用进程在内的进程都是通过Zygote进程fork出来的。
整个系统的android framework进程启动流程如下:
init进程 –> Zygote进程 –> SystemServer进程 –>各种应用进程
那么SystemServer进程是做什么用的呢?
其实SystemServer进程主要的作用是启动各种系统服务,比如ActivityManagerService,PackageManagerService,WindowManagerService等服务,我们平时熟知的各种系统性的服务其实都是在SystemServer进程中启动的,而当我们的应用需要使用各种系统服务的时候其实也是通过与SystemServer进程通讯获取各种服务对象的句柄的进而执行相应的操作的。
我们知道SystemServer进程其实也是由Zygote进程fork出来的,并且执行其main方法,那么这里我们看一下SystemServer的main方法的执行逻辑:
1 | /** |
然后我们查看SystemServer.run方法的实现:
1 | private void run() { |
代码比较多,慢慢看,先看一下第一段逻辑:
1 | if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) { |
if (!SystemProperties.get(“persist.sys.language”).isEmpty()) {
final String languageTag = Locale.getDefault().toLanguageTag();
1 | SystemProperties.set("persist.sys.locale", languageTag); |
下面的主要是设置虚拟机运行内存,加载运行库,设置SystemServer的异步消息,具体的异步消息机制可参见: android源码解析之(二)–>异步消息机制
然后下面的代码首先调用createSystemContext()方法:
1 | private void createSystemContext() { |
可以看到在SystemServer进程中也存在着Context对象,并且是通过ActivityThread.systemMain方法创建context的,这一部分的逻辑以后会通过介绍Activity的启动流程来介绍,这里就不在扩展,这里我们只知道在SystemServer进程中也需要创建Context对象。
然后通过SystemServiceManager的构造方法创建了一个新的SystemServiceManager对象,我们知道SystemServer进程主要是用来构建系统各种service服务的,而SystemServiceManager就是这些服务的管理对象。将SystemServiceManager对象保存到SystemServer进程中的一个数据结构中。
1 | // Create the system service manager. |
最后开始执行:
1 | // Start services. |
里面主要涉及了是三个方法:
startBootstrapServices()
主要用于启动系统Boot级服务
1 | private void startBootstrapServices() { |
首先执行:
1 | Installer installer = mSystemServiceManager.startService(Installer.class); |
mSystemServiceManager是系统服务管理对象,在main方法中已经创建完成,这里我们看一下其startService方法的具体实现:
1 | public <T extends SystemService> T startService(Class<T> serviceClass) { |
可以看到我们通过反射器构造方法创建出服务类,然后添加到SystemServiceManager的服务列表数据中,最后调用了service.onStart()方法,因为我们传递的是Installer.class,我们这里我们查看一下Installer.java的onStart方法:
1 | @Override |
Installer类是系统安装apk时的一个服务类,继承SystemService(系统服务的一个抽象接口),我们需要在启动完成Installer服务之后才能启动其他的系统服务。
然后查看waitForConnection()方法:
1 | public void waitForConnection() { |
通过追踪代码可以发现,其在不断的通过ping命令连接Zygote进程(SystemServer和Zygote进程通过socket方式通讯,其他进程通过Binder方式通讯);
在开始执行启动服务之前总是会先尝试通过socket方式连接Zygote进程,在成功连接之后才会开始启动其他服务。
1 | // Activity manager runs the show. |
这段代码主要是用于启动ActivityManagerService服务,并为其设置SysServiceManager和Installer。ActivityManagerService是系统中一个非常重要的服务,Activity,service,Broadcast,contentProvider都需要通过其与系统交互。
首先看一下Lifecycle类的定义:
1 | public static final class Lifecycle extends SystemService { |
可以看到其是ActivityManagerService的一个静态内部类,在其构造方法中会创建一个ActivityManagerService,通过刚刚对Installer服务的分析我们知道,SystemServiceManager的startService方法会调用服务的onStart()方法,而在Lifecycle类的定义中我们看到其onStart()方法直接调用了mService.start()方法,mService是Lifecycle类中对ActivityManagerService的引用,所以我们可以看一下ActivityManagerService的start方法的实现:
1 | private void start() { |
由于ActivityManagerService的创建过程比较复杂这里不做过多的分析了,主要是在其构造方法中初始化了一些变量。然后是启动PowerManagerService服务:
1 | mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class); |
启动方式跟上面的ActivityManagerService服务相似都会调用其构造方法和onStart方法,PowerManagerService主要用于计算系统中和Power相关的计算,然后决策系统应该如何反应。同时协调Power如何与系统其它模块的交互,比如没有用户活动时,屏幕变暗等等。
然后是启动LightsService服务
1 | mSystemServiceManager.startService(LightsService.class); |
主要是手机中关于闪光灯,LED等相关的服务;也是会调用LightsService的构造方法和onStart方法;
然后是启动DisplayManagerService服务
1 | mDisplayManagerService = mSystemServiceManager.startService(DisplayManagerService.class); |
主要是手机显示方面的服务;
然后是启动PackageManagerService,该服务也是android系统中一个比较重要的服务,包括多apk文件的安装,解析,删除,卸载等等操作。
1 | Slog.i(TAG, "Package Manager"); |
可以看到PackageManagerService服务的启动方式与其他服务的启动方式有一些区别,直接调用了PackageManagerService的静态main方法,这里我们看一下其main方法的具体实现:
1 | public static PackageManagerService main(Context context, Installer installer, |
可以看到也是直接使用new的方式创建了一个PackageManagerService对象,并在其构造方法中初始化相关变量,最后调用了ServiceManager.addService方法,主要是通过Binder机制与JNI层交互,这里不再扩展。
然后启动UserManagerService和SensorService,至此startBootstrapServices方法执行完成。
startCoreServices()
1 | /** |
可以看到这里启动了BatteryService(电池相关服务),UsageStatsService,WebViewUpdateService服务等。
startOtherServices()
startOtherServices方法,主要用于启动系统中其他的服务,代码很多,这里就不贴代码了,启动的流程和ActivityManagerService的流程类似,会调用服务的构造方法与onStart方法初始化变量。
总结
- SystemServer进程是android中一个很重要的进程由Zygote进程启动;
- SystemServer进程主要用于启动系统中的服务;
- SystemServer进程启动服务的启动函数为main函数;
- SystemServer在执行过程中首先会初始化一些系统变量,加载类库,创建Context对象,创建SystemServiceManager对象等之后才开始启动系统服务;
- SystemServer进程将系统服务分为三类:boot服务,core服务和other服务,并逐步启动
- SertemServer进程在尝试启动服务之前会首先尝试与Zygote建立socket通讯,只有通讯成功之后才会开始尝试启动服务;
- 创建的系统服务过程中主要通过SystemServiceManager对象来管理,通过调用服务对象的构造方法和onStart方法初始化服务的相关变量;