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程序主要用于啟動系統中的服務