文章目录
SystemServer、SystemServiceManger、SystemService、serviceManager的关系
- 管理机制:SystemServer 通过 SystemServiceManager 管理所有实现了 SystemService 的服务。每个服务初始化后会将自身注册到 ServiceManager,以实现跨进程调用 。
- SystemServer 进程:它是一个包含众多服务的进程,像常见的 AMS(Activity Manager Service,活动管理器服务 )、PKMS(Package Manager Service,包管理器服务 )、WMS(Window Manager Service,窗口管理器服务 )等,大概有 80 多个。
- SystemServiceManager 作用:专门用于管理 SystemServer 中的服务,这些服务基本都实现了 SystemService 类,使得 SystemServiceManager 能够对其进行管理 。
- App 调用方式:当 App 需要调用 systemService 时,在 SystemServer 启动 AMS、WMS、PKMS 等服务时,会将它们添加到 ServiceManager 中,供外部 app 进程调用。ServiceManager 是独立进程,负责管理服务列表。
SystemServer进程的执行
frameworks/base/services/java/com/android/server/SystemServer.java
/**
* The main entry point from zygote.
*/
public static void main(String[] args) {
new SystemServer().run();
}
private void run() {
...
// Initialize native services.
System.loadLibrary("android_servers");
...
// Initialize the system context.
createSystemContext();
// Call per-process mainline module initialization.
ActivityThread.initializeMainlineModules();
// Create the system service manager.
mSystemServiceManager = new SystemServiceManager(mSystemContext);
...
// Start services.
try {
t.traceBegin("StartServices");
startBootstrapServices(t);
startCoreServices(t);
startOtherServices(t);
startApexServices(t);
} catch (Throwable ex) {
Slog.e("System", "******************************************");
Slog.e("System", "************ Failure starting system services", ex);
throw ex;
} finally {
t.traceEnd(); // StartServices
}
}
包含的Service
startBootstrapServices
/**
* Starts the small tangle of critical services that are needed to get the system off the
* ground. These services have complex mutual dependencies which is why we initialize them all
* in one place here. Unless your service is also entwined in these dependencies, it should be
* initialized in one of the other functions.
*/
private void startBootstrapServices(@NonNull TimingsTraceAndSlog t) {
....
// FileIntegrityService responds to requests from apps and the system. It needs to run after
// the source (i.e. keystore) is ready, and before the apps (or the first customer in the
// system) run.
mSystemServiceManager.startService(FileIntegrityService.class);
// 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);
// In some cases after launching an app we need to access device identifiers,
// therefore register the device identifier policy before the activity manager.
mSystemServiceManager.startService(DeviceIdentifiersPolicyService.class);
// Uri Grants Manager.
mSystemServiceManager.startService(UriGrantsManagerService.Lifecycle.class);
// Tracks rail data to be used for power statistics.
mSystemServiceManager.startService(PowerStatsService.class);
...
}
startCoreServices
/**
* Starts some essential services that are not tangled up in the bootstrap process.
*/
private void startCoreServices(@NonNull TimingsTraceAndSlog t) {
t.traceBegin("startCoreServices");
// Service for system config
t.traceBegin("StartSystemConfigService");
mSystemServiceManager.startService(SystemConfigService.class);
t.traceEnd();
t.traceBegin("StartBatteryService");
// Tracks the battery level. Requires LightService.
mSystemServiceManager.startService(BatteryService.class);
t.traceEnd();
// Tracks application usage stats.
t.traceBegin("StartUsageService");
mSystemServiceManager.startService(UsageStatsService.class);
mActivityManagerService.setUsageStatsManager(
LocalServices.getService(UsageStatsManagerInternal.class));
t.traceEnd();
// Tracks whether the updatable WebView is in a ready state and watches for update installs.
if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_WEBVIEW)) {
t.traceBegin("StartWebViewUpdateService");
mWebViewUpdateService = mSystemServiceManager.startService(WebViewUpdateService.class);
t.traceEnd();
}
// Tracks and caches the device state.
t.traceBegin("StartCachedDeviceStateService");
mSystemServiceManager.startService(CachedDeviceStateService.class);
t.traceEnd();
// Tracks cpu time spent in binder calls
t.traceBegin("StartBinderCallsStatsService");
mSystemServiceManager.startService(BinderCallsStatsService.LifeCycle.class);
t.traceEnd();
...
}
/**
* Starts a miscellaneous grab bag of stuff that has yet to be refactored and organized.
*/
private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
...
ServiceManager.addService("sec_key_att_app_id_provider",
new KeyAttestationApplicationIdProviderService(context));
t.traceEnd();
t.traceBegin("StartKeyChainSystemService");
mSystemServiceManager.startService(KeyChainSystemService.class);
t.traceEnd();
t.traceBegin("StartBinaryTransparencyService");
mSystemServiceManager.startService(BinaryTransparencyService.class);
t.traceEnd();
t.traceBegin("StartSchedulingPolicyService");
ServiceManager.addService("scheduling_policy", new SchedulingPolicyService());
t.traceEnd();
// TelecomLoader hooks into classes with defined HFP logic,
// so check for either telephony or microphone.
if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_MICROPHONE)
|| mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELECOM)
|| mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
t.traceBegin("StartTelecomLoaderService");
mSystemServiceManager.startService(TelecomLoaderService.class);
t.traceEnd();
}
t.traceBegin("StartTelephonyRegistry");
telephonyRegistry = new TelephonyRegistry(
context, new TelephonyRegistry.ConfigurationProvider());
ServiceManager.addService("telephony.registry", telephonyRegistry);
t.traceEnd();
t.traceBegin("StartEntropyMixer");
mEntropyMixer = new EntropyMixer(context);
t.traceEnd();
mContentResolver = context.getContentResolver();
// The AccountManager must come before the ContentService
t.traceBegin("StartAccountManagerService");
mSystemServiceManager.startService(ACCOUNT_SERVICE_CLASS);
t.traceEnd();
...
}
安卓13中,手动查询SystemServer中的service数量,如下有183个:
cat frameworks/base/services/java/com/android/server/SystemServer.java | grep startService | wc -l
183
SystemServer启动服务的流程
startService
所有的服务都是通过这个函数启动的
- getConstructor是调的反射进行service构造
- service创建后,会调用publishBinderService把它注册到serviceManager中
- 通过addService把service添加到sLocalServiceObjects中
startService的流程:
startServicer用反射:方便扩展及管理,不然90个service就要实现90种创建的接口。
publishBinderService:注册到serviceManager中,由它统一管理。
addService到LocalServices: 方便Framework层平级的调用。
上层调用service,是通过binder来访问其中功能的,如下图,具体可以参照另外一篇binder的文章
Framework学习系列文章
Android Framework学习一:系统框架、启动过程
Android Framework学习二:Activity创建及View绘制流程
Android Framework学习三:zygote
Android Framework学习四:APP速度优化
Android Framework学习五:APP启动过程原理及速度优化
Android Framework学习六:Binder原理
Android Framework学习七:Handler、Looper、Message
Android Framework学习八:SystemServer及startService原理
作者:帅得不敢出门