概要
SystemServer 由 ZygoteInit 創建的第一個子進程,負責系統管理
framework\base\services\java\com\android\server\SystemServer.java
SystemServer.java中其實包含了兩個 class, SystemServer 和 ServerThread。
SystemServer 負責提供 main() 接口,ServerThread是工作的實體,雖然名字中有個Thread,但它並沒有創建一個Thread。
1, SystemServer main()
SystemServer main函數的工作內容:
設置優先級
SystemProperties.set("persist.sys.dalvik.vm.lib", VMRuntime.getRuntime().vmLibrary());
設置時間
SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);
SamplingProfilerIntegration.start()
開啓定時任務 writeSnapshot
timer.schedule(new TimerTask() {
@Override
public void run() {
SamplingProfilerIntegration.writeSnapshot("system_server", null);
}
}, SNAPSHOT_INTERVAL, SNAPSHOT_INTERVAL);
// Mmmmmm... more memory!
dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
// The system server has to run all of the time, so it needs to be
// as efficient as possible with its memory usage.
VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
加載 android_servers
System.loadLibrary("android_servers");
開始 ServerThread,這裏雖然用了一個Thread的名字,但其實他並不是一個Thread
// This used to be its own separate thread, but now it is
// just the loop we run on the main thread.
ServerThread thr = new ServerThread();
thr.initAndLoop();
2, ServerThread 的內容
ServerThread 中主要涉及了系統中各種服務的管理,內容很豐富。看看它的成員變量就知道了:
Installer installer = null;
AccountManagerService accountManager = null; (聯繫人)賬戶管理
ContentService contentService = null; ContentProvider服務,跨進程數據交換
LightsService lights = null; 自然光感應傳感器服務
PowerManagerService power = null; 電源管理服務
DisplayManagerService display = null; 顯示管理服務
BatteryService battery = null; 電池管理服務
VibratorService vibrator = null; 震動服務
AlarmManagerService alarm = null; 定時服務提醒
MountService mountService = null; 掛載服務
NetworkManagementService networkManagement = null; 網絡管理
NetworkStatsService networkStats = null; 網絡狀態管理
NetworkPolicyManagerService networkPolicy = null; 網絡策略管理
ConnectivityService connectivity = null; 網絡連接管理
WifiP2pService wifiP2p = null; Wifip2p服務
WifiService wifi = null; Wifi服務
NsdService serviceDiscovery= null; 網絡服務發現服務管理(Network Service Discovery Service)
IPackageManager pm = null; 程序包管理服務
Context context = null; Context
WindowManagerService wm = null; 窗口管理服務
BluetoothManagerService bluetooth = null; 藍牙管理服務
DockObserver dock = null; Monitors for a docking station,針對擴展槽的服務
UsbService usb = null; USB管理服務
SerialService serial = null; 串口管理服務
TwilightService twilight = null; 指出用戶當前所在位置是否爲晚上,可以用來調整夜間模式
UiModeManagerService uiMode = null; 管理UI的模式 例如(夜間模式和行車模式)
RecognitionManagerService recognition = null; 身份識別服務
NetworkTimeUpdateService networkTimeUpdater = null; 網絡時間更新服務
CommonTimeManagementService commonTimeMgmtService = null; 主要是配置 native Common Time service
InputManagerService inputManager = null; 輸入法管理服務
TelephonyRegistry telephonyRegistry = null; 電話服務,註冊電話模塊事件響應
ConsumerIrService consumerIr = null; 紅外管理服務,可以通過紅外遠程控制其他電子產品
在 ServerThread中最主要的工作就是創建一個服務,並把它加入ServiceManager中。
這樣代碼中的邏輯並不複雜,主要就是創建服務的過程。
例如電源管理:
power = new PowerManagerService();
ServiceManager.addService(Context.POWER_SERVICE, power);
顯示管理:
display = new DisplayManagerService(context, wmHandler);
ServiceManager.addService(Context.DISPLAY_SERVICE, display, true);
當各個服務的對象創建完成後,這些對象也會暴露出來供外層使用。
// These are needed to propagate to the runnable below.
final Context contextF = context;
final MountService mountServiceF = mountService;
final BatteryService batteryF = battery;
final NetworkManagementService networkManagementF = networkManagement;
final NetworkStatsService networkStatsF = networkStats;
final NetworkPolicyManagerService networkPolicyF = networkPolicy;
final ConnectivityService connectivityF = connectivity;
final DockObserver dockF = dock;
final UsbService usbF = usb;
final TwilightService twilightF = twilight;
final UiModeManagerService uiModeF = uiMode;
final AppWidgetService appWidgetF = appWidget;
final WallpaperManagerService wallpaperF = wallpaper;
final InputMethodManagerService immF = imm;
final RecognitionManagerService recognitionF = recognition;
final LocationManagerService locationF = location;
final CountryDetectorService countryDetectorF = countryDetector;
final NetworkTimeUpdateService networkTimeUpdaterF = networkTimeUpdater;
final CommonTimeManagementService commonTimeMgmtServiceF = commonTimeMgmtService;
final TextServicesManagerService textServiceManagerServiceF = tsms;
final StatusBarManagerService statusBarF = statusBar;
final DreamManagerService dreamyF = dreamy;
final AssetAtlasService atlasF = atlas;
final InputManagerService inputManagerF = inputManager;
final TelephonyRegistry telephonyRegistryF = telephonyRegistry;
final PrintManagerService printManagerF = printManager;
當服務就緒後,通知activity manager可以運行第三方的程序了,並且將各個服務的狀態置爲Ready。
例如:調用 mountServiceF.systemReady() ,但systemReady()函數中也做了一些各個Service自己的初始化工作。
可以跟着不同的模塊再去整理。
ActivityManagerService.self().systemReady(new Runnable() {
public void run() {
Slog.i(TAG, "Making services ready");
try {
ActivityManagerService.self().startObservingNativeCrashes();
} catch (Throwable e) {
reportWtf("observing native crashes", e);
}
if (!headless) {
startSystemUi(contextF);
}
try {
if (mountServiceF != null) mountServiceF.systemReady();
} catch (Throwable e) {
reportWtf("making Mount Service ready", e);
}
try {
if (batteryF != null) batteryF.systemReady();
} catch (Throwable e) {
reportWtf("making Battery Service ready", e);
}
try {
if (networkManagementF != null) networkManagementF.systemReady();
} catch (Throwable e) {
reportWtf("making Network Managment Service ready", e);
}
.................................
.................................
}