前言

System Server是android 基本服务的提供者,是android系统运行的最基本需求,所有server运行在一个叫system_process的进程中,system_process进程是android java虚拟机跑的第一个进程,从Zygote 创建而来,是andorid系统最重要的java虚拟机。可以说,整个android系统的业务都是围绕system server而展开,所以,当system_process死掉了,手机必须重启。

System server概要

System server主要包含以下服务:

1、 EntropyService, 熵服务,用于产生随机数;

2、 PowerManagerService, 电源管理服务;

3、 ActivityMangerService Activity,管理服务;

4、 TelephonyRegistry, 电话服务,电话底层通知服务;

5、 PackageManagerService, 程序包管理服务;

6、 AccountManagerService, 联系人账户管理服务;

7、 ContentService, 内容提供器服务,提供跨进程数据交换;

8、 LightService, 光感应传感器服务;

9、 BatteryService,电池服务;

10、 VibrateService,震动器服务;

11、 AlarmManagerService,闹钟服务;

12、 WindowManagerService,窗口管理服务;

13、 BluetoothService,蓝牙服务;

14、 InputMethodManagerService,输入法服务;

15、 AccessibilityManagerService, 辅助器管理程序服务;

16、 DevicePolicyManagerService,提供系统级别的设置及属性;

17、 StatusBarManagerService,状态栏管理服务;

18、 ClipboardService,粘贴板服务;

19、 NetworkManagerService,网络管理服务;

20、 TextServicesManagerService,用户管理服务;

21、 NetworkStatsService,手机网络状态服务;

22、 NetworkPolicyManagerService,low-level网络策略服务;

23、 WifiP2pService,Wifi点对点服务;

24、 WifiService,Wifi服务;

25、 ConnectivityService,网络连接状态服务;

26、 MountService,磁盘加载服务;

27、 NotificationManagerService,通知管理服务;

28、 DeviceStorageMonitorService,存储设备容量监听服务;

29、 LocationManagerService,位置管理服务;

30、 CountryDetectorService,检查用户当前所在国家服务;

31、 SearchManagerService,搜索管理服务;

32、 DropBoxManagerService,系统日志文件管理服务;

33、 WallpaperManagerService,壁纸管理服务;

34、 AudioService,AudioFlinger上层封装音频管理服务;

35、 UsbService,USB Host 和 device管理服务;

36、 UiModeManagerService,UI模式管理服务;

37、 BackupManagerService,备份服务;

38、 AppWidgetService,应用桌面部件服务;

39、 RecognitionManagerService,身份识别服务;

40、 DiskStatsService,磁盘统计服务;

41、 SamplingProfilerService,性能统计服务;

42、 NetworkTimeUpdateService,网络时间更新服务;

43、 InputManagerService,输入管理服务;

44、 FingerprintService,指纹服务;

45、 DreamManagerService, dreams服务;

46、 HdmiControlService, HDMI控制服务;

47、 SsvService,SIM卡状态和转换服务;

以上所列系统服务不代表最新Android系统的所有系统服务。

System server启动的入口

这些服务那么重要,它们什么时候启动?都做了些什么事情?等等这些问题是本文以及大纲下面的文章将会一一道来。

了解这些系统服务之前,先来了解一个重量级的超级类SystemServer.java, 这个类在AOSP中的路径是:frameworks/base/services/java/com/android/server/SystemServer.java,在上文中提到,从Zygote创建system_process进程时,便实例化了该类。熟悉java的开发者都知道,启动某个java程序时,最先调用的就是声明了main()方法的类。所以SystemServer.java被实例化后,便调用了main()方法。首先看看SystemServer.java的构造方法:

public SystemServer() {
// Check for factory test mode.
mFactoryTestMode = FactoryTest.getMode();
}

构造方法里面只做了从属性系统中读取了工厂测试模式,这个不在本文以及本大纲的文章中赘述的内容,这里就不在叙述了。

下面直接进入main()方法:

public static void main(String[] args) {
new SystemServer().run();
}

直接调用了类内的run()方法,继续跟踪:

private void run() {
try {
// AndroidRuntime using the same set of system properties, but only the system_server
// Initialize the system context.
createSystemContext(); // Create the system service manager.
mSystemServiceManager = new SystemServiceManager(mSystemContext);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
} finally {
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
} // Start services.
try {
Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartServices");
startBootstrapServices();
startCoreServices();
startOtherServices();
} catch (Throwable ex) {
Slog.e("System", "******************************************");
Slog.e("System", "************ Failure starting system services", ex);
throw ex;
} finally {
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
}
.....
}

上述代码中,首先创建一个system context,这个对象在整个system server中使用非常频繁。接着创建一个SystemServiceManager.java的实例,顾名思义就是SystemService的管理者,管理所有的SystemService。代码中把接着创建一个SystemServiceManager的实例通过LocalServices.addService(SystemServiceManager.class, mSystemServiceManager)语句设置到LocalService中,LocalServices的功能类似SystemServiceManager,不同点在于LocalServices专门用来管理system_process进程中的SystemService,也就没有真正的跨进程通信,也就没有Binder对象。

System server启动的过程

接着就是启动各种系统服务,在这里分三种类型的SystemService,依次启动,分别对应三个方法:

        startBootstrapServices();
startCoreServices();
startOtherServices();

这三个方法的顺序不能混乱,一定要按照上述顺序依次执行,先看startBootstrapServices()中启动了那些系统服务

private void startBootstrapServices() {
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); Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "InitPowerManagement");
mActivityManagerService.initPowerManagement();
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER); // 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.
..... traceBeginAndSlog("StartUserManagerService");
mSystemServiceManager.startService(UserManagerService.LifeCycle.class);
......
}

上述代码可以看到,这里启动了

  • Installer
  • ActivityManagerService.Lifecycle
  • PowerManagerService
  • LightsService
  • DisplayManagerService
  • UserManagerService

启动这些SystemService都是通过SystemServiceManager的startService()的方式启动,启动完成后会回调SystemService的onStart()方法。继续看第二个方法startCoreServices()启动了那些系统服务

private void startCoreServices() {
// Tracks the battery level. Requires LightService.
mSystemServiceManager.startService(BatteryService.class); // Tracks application usage stats.
mSystemServiceManager.startService(UsageStatsService.class);
mActivityManagerService.setUsageStatsManager(
LocalServices.getService(UsageStatsManagerInternal.class)); // Tracks whether the updatable WebView is in a ready state and watches for update installs.
mWebViewUpdateService = mSystemServiceManager.startService(WebViewUpdateService.class);
}

startCoreServices()中启动了

  • BatteryService

  • UsageStatsService

  • WebViewUpdateService

上文中提到,startBootstrapServices()必须在startCoreServices()前面执行,这里就能找到一个原因,startCoreServices()中用到mActivityManagerService对象,而该对象在startBootstrapServices()中被实例化,如果先调用startCoreServices(),mActivityManagerService对象便会发生NullPointerException的RuntimeException异常。

接着就是startOtherServices()中启动的service了,先看代码

private void startOtherServices() {
try {
ServiceManager.addService("scheduling_policy", new SchedulingPolicyService()); mSystemServiceManager.startService(TelecomLoaderService.class); traceBeginAndSlog("StartTelephonyRegistry");
telephonyRegistry = new TelephonyRegistry(context);
ServiceManager.addService("telephony.registry", telephonyRegistry);
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER); mEntropyMixer = new EntropyMixer(context); mContentResolver = context.getContentResolver(); mSystemServiceManager.startService(CameraService.class); mSystemServiceManager.startService(ACCOUNT_SERVICE_CLASS); mSystemServiceManager.startService(CONTENT_SERVICE_CLASS); mActivityManagerService.installSystemProviders(); vibrator = new VibratorService(context);
ServiceManager.addService("vibrator", vibrator); consumerIr = new ConsumerIrService(context);
ServiceManager.addService(Context.CONSUMER_IR_SERVICE, consumerIr); mSystemServiceManager.startService(AlarmManagerService.class); final Watchdog watchdog = Watchdog.getInstance();
watchdog.init(context, mActivityManagerService); traceBeginAndSlog("StartInputManagerService");
inputManager = new InputManagerService(context);
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER); wm = WindowManagerService.main(context, inputManager,
mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL,
!mFirstBoot, mOnlyCore);
ServiceManager.addService(Context.WINDOW_SERVICE, wm);
ServiceManager.addService(Context.INPUT_SERVICE, inputManager); mSystemServiceManager.startService(VrManagerService.class); mActivityManagerService.setWindowManager(wm); inputManager.setWindowManagerCallbacks(wm.getInputMonitor());
inputManager.start(); // TODO: Use service dependencies instead.
mDisplayManagerService.windowManagerAndInputReady(); .....
mSystemServiceManager.startService(BluetoothService.class);
..... mSystemServiceManager.startService(MetricsLoggerService.class); mSystemServiceManager.startService(PinnerService.class);
} catch (RuntimeException e) {
}
// Bring up services needed for UI.
if (mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
mSystemServiceManager.startService(InputMethodManagerService.Lifecycle.class); traceBeginAndSlog("StartAccessibilityManagerService");
try {
ServiceManager.addService(Context.ACCESSIBILITY_SERVICE,
new AccessibilityManagerService(context));
} catch (Throwable e) {
}
}
if (mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
if (!disableStorage &&
!"0".equals(SystemProperties.get("system_init.startmountservice"))) {
try {
mSystemServiceManager.startService(MOUNT_SERVICE_CLASS);
mountService = IMountService.Stub.asInterface(
ServiceManager.getService("mount"));
} catch (Throwable e) {
reportWtf("starting Mount Service", e);
}
}
}
mSystemServiceManager.startService(UiModeManagerService.class);
..... if (mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
if (!disableNonCoreServices) {
try {
mSystemServiceManager.startService(LOCK_SETTINGS_SERVICE_CLASS);
lockSettings = ILockSettings.Stub.asInterface(
ServiceManager.getService("lock_settings"));
} catch (Throwable e) {
} if (!SystemProperties.get(PERSISTENT_DATA_BLOCK_PROP).equals("")) {
mSystemServiceManager.startService(PersistentDataBlockService.class);
} mSystemServiceManager.startService(DeviceIdleController.class);
mSystemServiceManager.startService(DevicePolicyManagerService.Lifecycle.class);
} if (!disableSystemUI) {
try {
statusBar = new StatusBarManagerService(context, wm);
ServiceManager.addService(Context.STATUS_BAR_SERVICE, statusBar);
} catch (Throwable e) {
}
} if (!disableNonCoreServices) {
try {
ServiceManager.addService(Context.CLIPBOARD_SERVICE,
new ClipboardService(context));
} catch (Throwable e) {
}
} if (!disableNetwork) {
try {
networkManagement = NetworkManagementService.create(context);
ServiceManager.addService(Context.NETWORKMANAGEMENT_SERVICE, networkManagement);
} catch (Throwable e) {
}
} if (!disableNonCoreServices && !disableTextServices) {
mSystemServiceManager.startService(TextServicesManagerService.Lifecycle.class);
} if (!disableNetwork) {
traceBeginAndSlog("StartNetworkScoreService");
try {
networkScore = new NetworkScoreService(context);
ServiceManager.addService(Context.NETWORK_SCORE_SERVICE, networkScore);
} catch (Throwable e) {
}
try {
networkStats = NetworkStatsService.create(context, networkManagement);
ServiceManager.addService(Context.NETWORK_STATS_SERVICE, networkStats);
} catch (Throwable e) {
} try {
networkPolicy = new NetworkPolicyManagerService(
context, mActivityManagerService,
(IPowerManager)ServiceManager.getService(Context.POWER_SERVICE),
networkStats, networkManagement);
ServiceManager.addService(Context.NETWORK_POLICY_SERVICE, networkPolicy);
} catch (Throwable e) {
} if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI_NAN)) {
mSystemServiceManager.startService(WIFI_NAN_SERVICE_CLASS);
} else {
Slog.i(TAG, "No Wi-Fi NAN Service (NAN support Not Present)");
}
mSystemServiceManager.startService(WIFI_P2P_SERVICE_CLASS);
mSystemServiceManager.startService(WIFI_SERVICE_CLASS);
mSystemServiceManager.startService(
"com.android.server.wifi.scanner.WifiScanningService"); if (!disableRtt) {
mSystemServiceManager.startService("com.android.server.wifi.RttService");
} if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_ETHERNET) ||
mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_HOST)) {
mSystemServiceManager.startService(ETHERNET_SERVICE_CLASS);
} traceBeginAndSlog("StartConnectivityService");
try {
connectivity = new ConnectivityService(
context, networkManagement, networkStats, networkPolicy);
ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity);
networkStats.bindConnectivityManager(connectivity);
networkPolicy.bindConnectivityManager(connectivity);
} catch (Throwable e) {
} try {
serviceDiscovery = NsdService.create(context);
ServiceManager.addService(
Context.NSD_SERVICE, serviceDiscovery);
} catch (Throwable e) {
reportWtf("starting Service Discovery Service", e);
} if (!disableNonCoreServices) {
try {
ServiceManager.addService(Context.UPDATE_LOCK_SERVICE,
new UpdateLockService(context));
} catch (Throwable e) {
}
} if (!disableNonCoreServices) {
mSystemServiceManager.startService(RecoverySystemService.class);
}
...... mSystemServiceManager.startService(NotificationManagerService.class);
notification = INotificationManager.Stub.asInterface(
ServiceManager.getService(Context.NOTIFICATION_SERVICE));
networkPolicy.bindNotificationManager(notification); mSystemServiceManager.startService(DeviceStorageMonitorService.class); if (!disableLocation) {
try {
location = new LocationManagerService(context);
ServiceManager.addService(Context.LOCATION_SERVICE, location);
} catch (Throwable e) {
}
try {
countryDetector = new CountryDetectorService(context);
ServiceManager.addService(Context.COUNTRY_DETECTOR, countryDetector);
} catch (Throwable e) {
}
} if (!disableNonCoreServices && !disableSearchManager) {
traceBeginAndSlog("StartSearchManagerService");
try {
mSystemServiceManager.startService(SEARCH_MANAGER_SERVICE_CLASS);
} catch (Throwable e) {
}
} mSystemServiceManager.startService(DropBoxManagerService.class); if (!disableNonCoreServices && context.getResources().getBoolean(
R.bool.config_enableWallpaperService)) {
mSystemServiceManager.startService(WALLPAPER_SERVICE_CLASS);
} mSystemServiceManager.startService(AudioService.Lifecycle.class); if (!disableNonCoreServices) {
mSystemServiceManager.startService(DockObserver.class); if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH)) {
mSystemServiceManager.startService(THERMAL_OBSERVER_CLASS);
}
}
try {
// Listen for wired headset changes
inputManager.setWiredAccessoryCallbacks(
new WiredAccessoryManager(context, inputManager));
} catch (Throwable e) {
} if (!disableNonCoreServices) {
if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_MIDI)) {
// Start MIDI Manager service
mSystemServiceManager.startService(MIDI_SERVICE_CLASS);
} if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_HOST)
|| mPackageManager.hasSystemFeature(
PackageManager.FEATURE_USB_ACCESSORY)) {
mSystemServiceManager.startService(USB_SERVICE_CLASS);
} if (!disableSerial) {
try {
// Serial port support
serial = new SerialService(context);
ServiceManager.addService(Context.SERIAL_SERVICE, serial);
} catch (Throwable e) {
}
} "StartHardwarePropertiesManagerService");
try {
hardwarePropertiesService = new HardwarePropertiesManagerService(context);
ServiceManager.addService(Context.HARDWARE_PROPERTIES_SERVICE,
hardwarePropertiesService);
} catch (Throwable e) {
Slog.e(TAG, "Failure starting HardwarePropertiesManagerService", e);
}
} mSystemServiceManager.startService(TwilightService.class); mSystemServiceManager.startService(JobSchedulerService.class); mSystemServiceManager.startService(SoundTriggerService.class); if (!disableNonCoreServices) {
if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_BACKUP)) {
mSystemServiceManager.startService(BACKUP_MANAGER_SERVICE_CLASS);
} if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_APP_WIDGETS)
|| context.getResources().getBoolean(R.bool.config_enableAppWidgetService)) {
mSystemServiceManager.startService(APPWIDGET_SERVICE_CLASS);
} if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_VOICE_RECOGNIZERS)) {
mSystemServiceManager.startService(VOICE_RECOGNITION_MANAGER_SERVICE_CLASS);
} if (GestureLauncherService.isGestureLauncherEnabled(context.getResources())) {
Slog.i(TAG, "Gesture Launcher Service");
mSystemServiceManager.startService(GestureLauncherService.class);
}
mSystemServiceManager.startService(SensorNotificationService.class);
mSystemServiceManager.startService(ContextHubSystemService.class);
} try {
ServiceManager.addService("diskstats", new DiskStatsService(context));
} catch (Throwable e) {
} if (!disableSamplingProfiler) {
traceBeginAndSlog("StartSamplingProfilerService");
try {
ServiceManager.addService("samplingprofiler",
new SamplingProfilerService(context));
} catch (Throwable e) {
}
} if (!disableNetwork && !disableNetworkTime) {
traceBeginAndSlog("StartNetworkTimeUpdateService");
try {
networkTimeUpdater = new NetworkTimeUpdateService(context);
ServiceManager.addService("network_time_update_service", networkTimeUpdater);
} catch (Throwable e) {
}
} traceBeginAndSlog("StartCommonTimeManagementService");
try {
commonTimeMgmtService = new CommonTimeManagementService(context);
ServiceManager.addService("commontime_management", commonTimeMgmtService);
} catch (Throwable e) {
}
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER); if (!disableNetwork) {
try {
CertBlacklister blacklister = new CertBlacklister(context);
} catch (Throwable e) {
}
} if (!disableNonCoreServices) {
mSystemServiceManager.startService(DreamManagerService.class);
} if (!disableNonCoreServices && ZygoteInit.PRELOAD_RESOURCES) {
traceBeginAndSlog("StartAssetAtlasService");
try {
atlas = new AssetAtlasService(context);
ServiceManager.addService(AssetAtlasService.ASSET_ATLAS_SERVICE, atlas);
} catch (Throwable e) {
}
} if (!disableNonCoreServices) {
ServiceManager.addService(GraphicsStatsService.GRAPHICS_STATS_SERVICE,
new GraphicsStatsService(context));
} if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_PRINTING)) {
mSystemServiceManager.startService(PRINT_MANAGER_SERVICE_CLASS);
} mSystemServiceManager.startService(RestrictionsManagerService.class); mSystemServiceManager.startService(MediaSessionService.class); if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_HDMI_CEC)) {
mSystemServiceManager.startService(HdmiControlService.class);
} if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_LIVE_TV)) {
mSystemServiceManager.startService(TvInputManagerService.class);
} if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)) {
mSystemServiceManager.startService(MediaResourceMonitorService.class);
} if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK)) {
mSystemServiceManager.startService(TvRemoteService.class);
} if (!disableNonCoreServices) {
traceBeginAndSlog("StartMediaRouterService");
try {
mediaRouter = new MediaRouterService(context);
ServiceManager.addService(Context.MEDIA_ROUTER_SERVICE, mediaRouter);
} catch (Throwable e) {
} if (!disableTrustManager) {
mSystemServiceManager.startService(TrustManagerService.class);
} if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) {
mSystemServiceManager.startService(FingerprintService.class);
}
}
// LauncherAppsService uses ShortcutService.
mSystemServiceManager.startService(ShortcutService.Lifecycle.class); mSystemServiceManager.startService(LauncherAppsService.class);
} if (!disableNonCoreServices && !disableMediaProjection) {
mSystemServiceManager.startService(MediaProjectionManagerService.class);
} if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH)) {
mSystemServiceManager.startService(WEAR_BLUETOOTH_SERVICE_CLASS);
}
mmsService = mSystemServiceManager.startService(MmsServiceBroker.class); // It is now time to start up the app processes... try {
vibrator.systemReady();
} catch (Throwable e) {
} if (lockSettings != null) {
try {
lockSettings.systemReady();
} catch (Throwable e) {
}
} // Needed by DevicePolicyManager for initialization
mSystemServiceManager.startBootPhase(SystemService.PHASE_LOCK_SETTINGS_READY); mSystemServiceManager.startBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY); Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakeWindowManagerServiceReady");
try {
wm.systemReady();
} catch (Throwable e) {
}
try {
// TODO: use boot phase
mPowerManagerService.systemReady(mActivityManagerService.getAppOpsService());
} catch (Throwable e) {
} try {
mPackageManagerService.systemReady();
} catch (Throwable e) {
} try {
// TODO: use boot phase and communicate these flags some other way
mDisplayManagerService.systemReady(safeMode, mOnlyCore);
} catch (Throwable e) {
}
mActivityManagerService.systemReady(new Runnable() {
@Override
public void run() {
try {
startSystemUi(context);
} catch (Throwable e) {
}
"MakeNetworkManagementServiceReady");
try {
if (networkManagementF != null) networkManagementF.systemReady();
} catch (Throwable e) {
reportWtf("making Network Managment Service ready", e);
}
"MakeNetworkStatsServiceReady");
try {
if (networkStatsF != null) networkStatsF.systemReady();
} catch (Throwable e) {
}
try {
if (networkPolicyF != null) networkPolicyF.systemReady();
} catch (Throwable e) {
reportWtf("making Network Policy Service ready", e);
}
"MakeConnectivityServiceReady");
try {
if (connectivityF != null) connectivityF.systemReady();
} catch (Throwable e) {
}
Watchdog.getInstance().start(); try {
if (locationF != null) locationF.systemRunning();
} catch (Throwable e) {
}
try {
if (countryDetectorF != null) countryDetectorF.systemRunning();
} catch (Throwable e) {
}
try {
if (networkTimeUpdaterF != null) networkTimeUpdaterF.systemRunning();
} catch (Throwable e) {
}
try {
if (commonTimeMgmtServiceF != null) {
commonTimeMgmtServiceF.systemRunning();
}
} catch (Throwable e) {
}
try {
if (atlasF != null) atlasF.systemRunning();
} catch (Throwable e) {
}
try {
// TODO(BT) Pass parameter to input manager
if (inputManagerF != null) inputManagerF.systemRunning();
} catch (Throwable e) {
}
try {
if (telephonyRegistryF != null) telephonyRegistryF.systemRunning();
} catch (Throwable e) {
reportWtf("Notifying TelephonyRegistry running", e);
}
try {
if (mediaRouterF != null) mediaRouterF.systemRunning();
} catch (Throwable e) {
} try {
if (mmsServiceF != null) mmsServiceF.systemRunning();
} catch (Throwable e) {
} try {
if (networkScoreF != null) networkScoreF.systemRunning();
} catch (Throwable e) {
}
}
});
}

这个方法中启动的service非常多,代码量非常巨大,所以可见,这个方法对于Android系统而言,不言也可知其重要性。由于代码量巨大,将startOtherServices()分成三部分解说:

1.startOtherServices()中启动了那些服务

  • SchedulingPolicyService

  • TelecomLoaderService

  • TelephonyRegistry

  • CameraService

  • AccountManagerService $LifecycleContentService$Lifecycle

  • VibratorService

  • ConsumerIrService

  • AlarmManagerService

  • InputManagerService

  • WindowManagerService

  • VrManagerService

  • MetricsLoggerService

  • PinnerService

  • InputMethodManagerService

  • AccessibilityManagerService

  • MountService \(LifecycleUiModeManagerServiceLockSettingsService\)Lifecycle

  • PersistentDataBlockService

  • DeviceIdleController

  • DevicePolicyManagerService

  • StatusBarManagerService

  • ClipboardService

  • NetworkManagementService

  • TextServicesManagerService

  • NetworkScoreService

  • NetworkStatsService

  • NetworkPolicyManagerService

  • WifiNanService

  • WifiP2pService

  • WifiService

  • WifiScanningService

  • RttService

  • EthernetService

  • ConnectivityService

  • NsdService

  • UpdateLockService

  • RecoverySystemService

  • NotificationManagerService

  • DeviceStorageMonitorService

  • LocationManagerService

  • CountryDetectorService

  • SearchManagerService\(LifecycleDropBoxManagerServiceWallpaperManagerService\)Lifecycle

  • AudioService.Lifecycle

  • DockObserver

  • ThermalObserver

  • MidiService\(LifecycleUsbService\)Lifecycle

  • SerialService

  • HardwarePropertiesManagerService

  • TwilightService

  • JobSchedulerService

  • SoundTriggerService

  • BackupManagerService$Lifecycle

  • AppWidgetService

  • VoiceInteractionManagerService

  • GestureLauncherService

  • SensorNotificationService

  • ContextHubSystemService

  • DiskStatsService

  • SamplingProfilerService

  • NetworkTimeUpdateService

  • CommonTimeManagementService

  • DreamManagerService

  • AssetAtlasService

  • GraphicsStatsService

  • PrintManagerService

  • RestrictionsManagerService

  • MediaSessionService

  • HdmiControlService

  • TvInputManagerService

  • MediaResourceMonitorService

  • TvRemoteService

  • MediaRouterService

  • TrustManagerService

    ShortcutService.Lifecycle

    LauncherAppsService

    MediaProjectionManagerService

    WearBluetoothService

    MmsServiceBroke

所列的这些服务,不一定全部都启动,系统会依照一些配置,选择性启动某些服务。如,因为Android系统会用于众多设备,手机,电视等等,当Android安装到电视设备是,TvInputManagerService、TvRemoteService这些service才会被启动,相反,手机设备时,这两个服务就不会被启动了。

2.不同的启动服务的方式

不知读者是否注意到,在startOtherServices()中存在和startBootstrapServices()、startCoreServices()中不同的启动service的方法,在startBootstrapServices()、startCoreServices()中见到SystemServiceManager.startService()和LocalServices.addService()的方式,而startOtherServices()中又多了ServiceManager.addService(),这三种方式有什么不同呢?

类型不同

  1. SystemServiceManager.startService()和LocalServices.addService()启动的系统服务是SystemService的子类,启动这些服务后会回调SystemService的onStart()方法。ServiceManager.addService()启动的系统服务是实现了Android IPC 的Binder的子类,这些服务启动后会调用systemReady()或systemRunning()方法。

  2. SystemServiceManager.startService()和ServiceManager.addService()中启动的服务需要Binder对象,而LocalServices.addService()却没有Binder对象。

  3. SystemServiceManager.startService()中启动的是需要关心lifecycle events的服务,而ServiceManager.addService()和LocalServices.addService()不关心lifecycle events。

  4. ServiceManager.addService()启动的服务本身是实现Binder通信的子类,而一般SystemServiceManager.startService()启动的服务是某个服务的内部类且这个内部类是SystemService的子类,如BackupManagerService$Lifecycle,在启动这个Lifecycle的内部类服务时,当回调onStart()方法时通过SystemService的publishBinderService()方法应用某个服务的Binder对象,且这个Binder的实现类也是某个服务的内部类。也就是说需要启动服务,而这个服务需要关心lifecycle events,所以不能通过ServiceManager.addService()的方式启动,然后在这个服务中声明一个实现了SystemService的子类Lifecycle,来接受lifecycle events,通过SystemServiceManager.startService()的方式启动这个服务,在SystemService的回调方法onStart()中应用这个服务的Binder对象。所以通过SystemServiceManager.startService()启动的服务,实际是启动了一个即需要关心lifecycle events,也需要像ServiceManager.addService()那样需要Binder对象的服务。

所以,其实在startBootstrapServices()中就已经通过ServiceManager.addService()的方式启动了一些特别特别重要的服务,如

private void startBootstrapServices() {
mPackageManagerService = PackageManagerService.main(mSystemContext, installer,
mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore);
} public static PackageManagerService main(Context context, Installer installer,
boolean factoryTest, boolean onlyCore) {
......
ServiceManager.addService("package", m);
return m;
}

3.通过ServiceManager.addService()启动的服务,会调用服务的systemReady()或systemRunning()方法,初始化一些需要在系统服务启动后的其它代码。如启动SystemUI。

全部启动所需要的服务以后,SystemServer便要进入了接受Message的循环中,等待Message的事件

Android系统之System Server大纲的更多相关文章

  1. (原)android系统下绑定Server的时候报MainActivity has leaked ServiceConnection的错误

    今天在android系统下根据官方的demo代码,我们需要启动一个服务,并绑定,但在程序启动以后,老是报错:   Activity MainActivity has leaked ServiceCon ...

  2. 替换Android系统镜像system.img的方法

    之前改动了Android的系统源代码的framework层代码,定制ROM.通过make之后会生成三个镜像文件userdata.img.system.img.ramdisk.img三个文件.这个时候我 ...

  3. Unity 如何将apk放到Android系统的system里

    有时我们需要用unity开发一款Android的系统软件,很坑,步骤如下: 1.用unity打包出来,签名. 2.用解压工具打开签过名的apk. 3.将lib里面的.so文件复制出来. 4.adb r ...

  4. Android系统驱动【转】

    本文转载自:http://www.hovercool.com/en/%E6%B7%BB%E5%8A%A0%E9%A9%B1%E5%8A%A8%E6%A8%A1%E5%9D%97#a_.E5.9B.9B ...

  5. Android系统加载Apk文件的时机和流程分析(1)--Android 4.4.4 r1的源码

    本文博客地址:https://blog.csdn.net/QQ1084283172/article/details/80982869 Android系统在启动时安装应用程序的过程,这些应用程序安装好之 ...

  6. android系统将普通应用升级为系统应用

    作为一名程序员,有的时候并不是使用软件,而是去改造软件,不仅仅只是会编程而已,还要满足客户的需求.这样,才能开发出符合客户需求的应用,在关于到涉及到android底层的应用的时候,手机就需要root了 ...

  7. Android系统Root原理初探(转)

    http://www.imooc.com/learn/126 chkconfig setup 解压update.zip这个文件,可发现它一般打包了如下这几个文件: 或者没有updates而是syste ...

  8. Android系统HAL开发实例

    1.前言 Android系统使用HAL这种设计模式,使得上层服务与底层硬件之间的耦合度降低,在文件: AOSP/hardware/libhardware/include/hardware/hardwa ...

  9. 图解Android - Zygote, System Server 启动分析

    Init 是所有Linux程序的起点,而Zygote于Android,正如它的英文意思,是所有java程序的'孵化池'(玩过星际虫族的兄弟都晓得的).用ps 输出可以看到 >adb shell ...

  10. Android系统进程间通信(IPC)机制Binder中的Client获得Server远程接口过程源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6633311 在上一篇文章中,我 们分析了And ...

随机推荐

  1. 对比python学julia(第四章:人工智能)--(第四节)绘画大师

    1.1.  项目简介 所谓图像风格迁移,是利用深度学习技术,将一幅风格图像输人卷积神经网络提取风格特征,再将其应用到另一幅内容图像上,从而生成一幅与风格囝像相仿的新图像.如果选取绘画大师的作品作为风格 ...

  2. 【转载】 校正Ubuntu时间为北京时间

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/qq_37421762/article/de ...

  3. NVIDIA公司推出的GPU运行环境下的机器人仿真环境(NVIDIA Isaac Gym)—— 到底实现了什么功能,意义价值又是什么???

    相关内容: NVIDIA公司推出的GPU运行环境下的机器人仿真环境(NVIDIA Isaac Gym)的安装--强化学习的仿真训练环境 ================================ ...

  4. Temperature 题解

    前言 题目链接:洛谷:SPOJ:Hydro & bzoj. 题意简述 有一个长度为 \(n\) 的序列,每个位置值的范围为 \([L_i, R_i]\) 内,求原序列可能的最长不降子串长度. ...

  5. WhaleStudio 2.6正式发布,WhaleTunnel同步性能与连接器数量再创新高!

    在这个数据驱动的大模型时代,数据集成的作用和意义愈发重要.数据不仅仅是信息的载体,更是推动企业决策和创新的关键因素.作为全球最流行的批流一体数据集成工具,WhaleTunnel随着WhaleStudi ...

  6. vue3:setup语法糖使用教程

    setup语法糖简介 直接在script标签中添加setup属性就可以直接使用setup语法糖了. 使用setup语法糖后,不用写setup函数:组件只需要引入不需要注册:属性和方法也不需要再返回,可 ...

  7. stm32中NVIC如何配置?

    1.NVIC优先级分组 2.初始化NVIC // NVIC优先级分组 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); // NVIC初始化 NVIC_I ...

  8. CH01_初识C++

    CH01_初识C++ 第一个C++程序 新建项目 新建文件 编写代码 #include <iostream> using namespace std; int main() { cout ...

  9. 通过JMX监控weblogic服务

    一.JMX简介 JMX是一种JAVA的正式规范,它主要目的是让程序有被管理的功能,那么怎么理解所谓的"被管理"呢?试想你开发了一个软件(如WEB网站),它是在24小时不间断运行的, ...

  10. Flex动态加载svg图片

    1.静态显示 在FLEX应用程序中可以使用SVG资源, 但只能象JPG和GIF那样作为一种图像引入, 而不包括SVG的一些高级特性, 而且无法在运行时加载, 只能在编译时静态加载,所以图片的大小无法改 ...