- 简单来说,systemServer就是系统用来启动各种service的入口,安卓系统在启动的时候, 会初 始化两个重要的部分,一个是zygote进程,另一个是由zygote进程fork出来的SystemServer进程,SystemServer会启动我们系统中所需要的一系列service,下面会做分析。
SystemServer由zygote进程启动。启动后,会调用SystemServer.main()方法,如下:
/**
* The main entry point from zygote.
*/
public static void main(String[] args) {
new SystemServer().run();
}
其中,创建SystemServer实例并调用run()方法,在run方法中进行了一些初始化的工作,例如调用createSystemContext创建系统Context。如下:
private void createSystemContext() {
ActivityThread activityThread = ActivityThread.systemMain();
mSystemContext = activityThread.getSystemContext();
mSystemContext.setTheme(android.R.style.Theme_DeviceDefault_Light_DarkActionBar);
}
也是在run()方法中,创建并启动了各项系统服务,
创建SystemServerManager:
// Create the system service manager.
mSystemServiceManager = new SystemServiceManager(mSystemContext);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
启动各种service:
try {
startBootstrapServices();
startCoreServices();
startOtherServices();
} catch (Throwable ex) {
Slog.e("System", "******************************************");
Slog.e("System", "************ Failure starting system services", ex);
throw ex;
}
从谷歌给我们的注释我们给出以上三个方法简单的解释:
- startBootstrapServices() 主要是用来开启一些系统级别的service,这些service具有高度的复杂的相互依赖关系,所以我们需要把他们的初始化放在同一个地方
- startCoreServices() 主要是启动一些核心的Service,但是这些service跟我们的bootservice没有相互依赖关系的,是相对独立的服务
- startOtherServices() 正如英文所示,这是一些费关键非核心的service
分析startBootstrapServices()方法
private void startBootstrapServices() {
// Wait for installd to finish starting up so that it has a chance to
// create critical directories such as /data/user with the appropriate
// permissions. We need this to complete before we initialize other services.
Installer installer = mSystemServiceManager.startService(Installer.class);
// Activity manager runs the show.
mActivityManagerService = mSystemServiceManager.startService(
ActivityManagerService.Lifecycle.class).getService();
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
mActivityManagerService.setInstaller(installer);
mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);
// Now that the power manager has been started, let the activity manager
// initialize power management features.
mActivityManagerService.initPowerManagement();
// Manages LEDs and display backlight so we need it to bring up the display.
mSystemServiceManager.startService(LightsService.class);
// Display manager is needed to provide display metrics before package manager
// starts up.
mDisplayManagerService = mSystemServiceManager.startService(DisplayManagerService.class);
// We need the default display before we can initialize the package manager.
mSystemServiceManager.startBootPhase(SystemService.PHASE_WAIT_FOR_DEFAULT_DISPLAY);
// Start the package manager.
Slog.i(TAG, "Package Manager");
mPackageManagerService = PackageManagerService.main(mSystemContext, installer,
mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore);
mFirstBoot = mPackageManagerService.isFirstBoot();
mPackageManager = mSystemContext.getPackageManager();
// Set up the Application instance for the system process and get started.
mActivityManagerService.setSystemProcess();
// The sensor service needs access to package manager service, app ops
// service, and permissions service, therefore we start it after them.
startSensorService();
}
这段代码的核心
traceBeginAndSlog("StartInstaller");
Installer installer = mSystemServiceManager.startService(Installer.class);
traceEnd();
// In some cases after launching an app we need to access device identifiers,
// therefore register the device identifier policy before the activity manager.
traceBeginAndSlog("DeviceIdentifiersPolicyService");
mSystemServiceManager.startService(DeviceIdentifiersPolicyService.class);
traceEnd();
这段代码的执行,就是为了先启动installer,这样android才有机会去创建一些关键的路径(data/user),这些都需要在其他Service启动前完成。其次,通过mSystemServiceManager.startService(DeviceIdentifiersPolicyService.class);这段代码,android需要注册当前的设备表示,以防有一些特殊的时候需要用到。
我们进入install类的onStart()方法一看
@Override
public void onStart() {
if (mIsolated) {
mInstalld = null;
} else {
connect();
}
}
private void connect() {
IBinder binder = ServiceManager.getService("installd");
if (binder != null) {
try {
binder.linkToDeath(new DeathRecipient() {
@Override
public void binderDied() {
Slog.w(TAG, "installd died; reconnecting");
connect();
}
}, 0);
} catch (RemoteException e) {
binder = null;
}
}
if (binder != null) {
mInstalld = IInstalld.Stub.asInterface(binder);
try {
invalidateMounts();
} catch (InstallerException ignored) {
}
} else {
Slog.w(TAG, "installd not found; trying again");
BackgroundThread.getHandler().postDelayed(() -> {
connect();
}, DateUtils.SECOND_IN_MILLIS);
}
}
我们可以看到,其实这就是一个递归。install通过调用connect,去判断installer是否被初始化。只有installer被初始化了,才会继续往下掉用,初始化其他的服务。
接下来,启动ActivityManagerService。我们通过代码分析:
mActivityManagerService = mSystemServiceManager.startService(
ActivityManagerService.Lifecycle.class).getService();
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
mActivityManagerService.setInstaller(installer);
我们开启ams传入的是 ActivityManagerService.Lifecycle.class,同时把installer传入给ams。我们进入AMS查看代码,可以看到
final void finishBooting() {
...
for (String abi : Build.SUPPORTED_ABIS) {
zygoteProcess.establishZygoteConnectionForAbi(abi);
final String instructionSet = VMRuntime.getInstructionSet(abi);
if (!completedIsas.contains(instructionSet)) {
try {
mInstaller.markBootComplete(VMRuntime.getInstructionSet(abi));
} catch (InstallerException e) {
Slog.w(TAG, "Unable to mark boot complete for abi: " + abi + " (" +
e.getMessage() +")");
}
completedIsas.add(instructionSet);
}
}
...
}
所以finishBooting是ams和zygote进行通讯的入口。
startBootstrapServices()方法中,还启动了很多其他的services,包括PowerManagerService、RecoverySystemService、LightsService、DisplayManagerService等等,这里就不重复做出分析了。
分析startCoreServices()方法
private void startCoreServices() {
// Records errors and logs, for example wtf()
traceBeginAndSlog("StartDropBoxManager");
mSystemServiceManager.startService(DropBoxManagerService.class);
traceEnd();
traceBeginAndSlog("StartBatteryService");
// Tracks the battery level. Requires LightService.
mSystemServiceManager.startService(BatteryService.class);
traceEnd();
// Tracks application usage stats.
traceBeginAndSlog("StartUsageService");
mSystemServiceManager.startService(UsageStatsService.class);
mActivityManagerService.setUsageStatsManager(
LocalServices.getService(UsageStatsManagerInternal.class));
traceEnd();
// Tracks whether the updatable WebView is in a ready state and watches for update installs.
traceBeginAndSlog("St
搜索工具
百度为您找到相关结果约229,000个
artWebViewUpdateService");
mWebViewUpdateService = mSystemServiceManager.startService(WebViewUpdateService.class);
traceEnd();
}
这个方法主要启动一些跟bootstrap进程无关的service,这些service是相对独立的服务。
分析startOtherServices()方法:
这个方法主要是为了整理或者重构一些杂七杂八的包,不太重要,不做分析。
总结:
- SystemServer进程是android中一个很重要的进程由Zygote进程启动;
- SystemServer进程主要用于启动系统中的服务;
- SystemServer进程启动服务的启动函数为main函数;
- SystemServer在执行过程中首先会初始化一些系统变量,加载类库,创建Context对象,创建SystemServiceManager对象等之后才开始启动系统服务;
- SystemServer进程将系统服务分为三类:boot服务,core服务和other服务,并逐步启动
- 创建的系统服务过程中主要通过SystemServiceManager对象来管理,通过调用服务对象的构造方法和onStart方法初始化服务的相关变量;
- 服务对象都有自己的异步消息对象,并运行在单独的线程中;