1, 基本概念
SystemServer是三大核心步骤中的最后一个,主要是各种服务的注册和启动。SystemServer进程由Zygote进程启动。zygoteInit的startSystemServer部分方法如下,
String args[] = { // SystemServer服务进程的信息
"--setuid=1000", // uid
"--setgid=1000", // gid
"--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,1021,1032,3001,3002,3003,3006,3007",
"--capabilities=" + capabilities + "," + capabilities,
"--nice-name=system_server", // 进程名
"--runtime-args",
"com.android.server.SystemServer", // 包名
};
在Zygote文章中已经分析了SystemServer进程的启动流程,在此就不多论述了,直接看SystemServer的main方法。
2, SystemServer
SystemServer的main方法如下,
public static void main(String[] args) {
new SystemServer().run();
}
Main函数很简单,仅仅构造一个SystemServer对象,然后调用run方法
run方法比较长,还是看大体步骤
1,调整时间
if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) {
Slog.w(TAG, "System clock is before 1970; setting to 1970.");
SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);
}
2,设置系统相关属性
if (!SystemProperties.get("persist.sys.language").isEmpty()) {
final String languageTag = Locale.getDefault().toLanguageTag();
SystemProperties.set("persist.sys.locale", languageTag);
SystemProperties.set("persist.sys.language", "");
SystemProperties.set("persist.sys.country", "");
SystemProperties.set("persist.sys.localevar", "");
}
public static void main(String[] args) {
new SystemServer().run();
}
3,设置环境
VMRuntime.getRuntime().setTargetHeapUtilization(0.8f); // 虚拟机堆栈的大小
Build.ensureFingerprintProperty(); // 初始化binder
Environment.setUserRequired(true);
BinderInternal.disableBackgroundScheduling(true);
// Prepare the main looper thread (this thread).
android.os.Process.setThreadPriority( // Binder,消息机制
android.os.Process.THREAD_PRIORITY_FOREGROUND);
android.os.Process.setCanSelfBackground(false);
Looper.prepareMainLooper();
// Initialize native services.
System.loadLibrary("android_servers"); // 加载JNI运行库
4,创建进程上下文
createSystemContext();
5,创建SystemServiceManager
mSystemServiceManager = new SystemServiceManager(mSystemContext);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
6,启动各种服务
// Start services.
try {
startBootstrapServices();
startCoreServices();
startOtherServices();
} catch (Throwable ex) {
Slog.e("System", "******************************************");
Slog.e("System", "************ Failure starting system services", ex);
throw ex;
}
7,开始循环处理消息。
Looper.loop();
3, 启动各种服务
服务分三类启动:
3.1 startBootstrapServices
ActivityManagerService | PowerManagerService | LightsService |
DisplayManagerService | RegionalizationService | PackageManagerService |
SensorService |
|
|
3.2 startCoreServices
BatteryService | UsageStatsService | WebViewUpdateService |
3.3 startOtherServices
SchedulingPolicyService | TelecomLoaderService | telephonyRegistry |
CameraService | AccountManagerService | ContentService |
VibratorService | ConsumerIrService | AlarmManagerService |
Watchdog | InputManagerService | WindowManagerService |
BluetoothService | InputMethodManagerService | MountService |
UiModeManagerService | LockSettingsService | DeviceIdleController |
StatusBarManagerService | ClipboardService | WifiP2pService |
NetworkScoreService | NetworkStatsService | WifiService |
WifiScanningService | RttService | EthernetService |
ConnectivityService | NsdService | UpdateLockService |
LocationManagerService | CountryDetectorService | SearchManagerService |
DropBoxManagerService | WallpaperManagerService | AudioService |
DockObserver | WiredAccessoryManager | MidiService |
UsbService | SerialService | TwilightService |
JobSchedulerService | BackupManagerService | AppWidgetService |
GestureLauncherService | DiskStatsService | SamplingProfilerService |
CertBlacklister | DreamManagerService | AssetAtlasService |
PrintManagerService | MediaSessionService | HdmiControlService |
MediaRouterService | TrustManagerService | FingerprintService |
LauncherAppsService | MmsServiceBroker |
|
AccessibilityManagerService PersistentDataBlockService DevicePolicyManagerService
NetworkManagementService TextServicesManagerService NetworkPolicyManagerService
NotificationManagerService DeviceStorageMonitorService VoiceInteractionManagerService
NetworkTimeUpdateService CommonTimeManagementService GraphicsStatsService
RestrictionsManagerService TvInputManagerService MediaProjectionManagerService
果然,一切皆服务啊!
4,小结
SystemServer进程是android中一个很重要的进程由Zygote进程启动
SystemServer进程主要用于启动系统中的服务