Android系統(tǒng)的心臟,SystemServer進(jìn)程及各系統(tǒng)服務(wù)之間的關(guān)系
SystemServer
SystemServer是Android系統(tǒng)中的一個重要進(jìn)程,是zygote fork的第一個進(jìn)程,負(fù)責(zé)啟動和管理系統(tǒng)中的各種服務(wù)。在Android系統(tǒng)中,SystemServer進(jìn)程的名稱為"system_server"。
SystemServer進(jìn)程啟動后,會加載SystemServer類并執(zhí)行其main函數(shù),main函數(shù)是SystemServer的入口點,負(fù)責(zé)啟動和初始化各種系統(tǒng)服務(wù)。在這個過程中,SystemServer會創(chuàng)建一個Looper和一個Handler,用于在主線程中處理消息和運(yùn)行任務(wù)。Looper是Android事件循環(huán)的一部分,負(fù)責(zé)在主線程中接收和分發(fā)消息,Handler則用于發(fā)送和處理消息。
SystemServer創(chuàng)建并初始化的系統(tǒng)服務(wù)包括ActivityManagerService(AMS)、PackageManagerService(PMS)、WindowManagerService(WMS)等。這些服務(wù)在Android系統(tǒng)中扮演著重要的角色,例如AMS負(fù)責(zé)管理應(yīng)用程序的生命周期和活動狀態(tài),PMS負(fù)責(zé)管理應(yīng)用程序包的安裝和卸載,WMS則負(fù)責(zé)管理窗口的創(chuàng)建、顯示和更新等。
在SystemServer的main函數(shù)中,還會啟動其他各種系統(tǒng)服務(wù),這些服務(wù)通過SystemServiceManager進(jìn)行管理和控制。SystemServer進(jìn)程會一直運(yùn)行,處理來自各種系統(tǒng)服務(wù)的消息,確保系統(tǒng)的正常運(yùn)行和穩(wěn)定性。
ServiceManager
ServiceManager是Android系統(tǒng)中的一個重要守護(hù)進(jìn)程,負(fù)責(zé)管理系統(tǒng)服務(wù)的注冊、查找和啟動。
- 「作用」:
提供服務(wù)注冊和查找:ServiceManager充當(dāng)了一個中央注冊表的角色,允許應(yīng)用程序和系統(tǒng)組件將自己注冊為服務(wù),并提供一個唯一的服務(wù)名稱。其他應(yīng)用程序可以通過ServiceManager查找并獲取已注冊的服務(wù),從而實現(xiàn)進(jìn)程間通信。
啟動和管理系統(tǒng)進(jìn)程:ServiceManager還負(fù)責(zé)啟動和管理一些重要的系統(tǒng)進(jìn)程,例如系統(tǒng)服務(wù)(如Telephony服務(wù)、Media服務(wù)等),以及其他一些重要的系統(tǒng)組件。
實現(xiàn)Binder機(jī)制:Android系統(tǒng)采用Binder作為進(jìn)程間通信(IPC)的機(jī)制,ServiceManager是Binder通信的關(guān)鍵組件之一。
- 「權(quán)限」:
ServiceManager的訪問權(quán)限較高,一般只有系統(tǒng)級應(yīng)用或者具有系統(tǒng)權(quán)限的應(yīng)用程序才能夠使用ServiceManager進(jìn)行服務(wù)的注冊和查詢。
- 「與Binder的關(guān)系」:
ServiceManager是Binder的守護(hù)進(jìn)程,在Android上如果ServiceManager掛掉,所有采用Binder通信的進(jìn)程服務(wù)都會受到影響。ServiceManager本身也是一個Binder服務(wù),其handle固定為0。應(yīng)用程序相要通過Binder向一個service發(fā)送數(shù)據(jù),必須先通過ServiceManager獲取該service的handle,然后才能通過binder驅(qū)動與service通信。
- 「啟動流程」:
在Android系統(tǒng)的啟動過程中,SystemServer進(jìn)程在啟動時會啟動ServiceManager,并將各種系統(tǒng)服務(wù)注冊到ServiceManager中。
protected final void publishBinderService(String name, IBinder service, boolean allowIsolated) {
ServiceManager.addService(name, service, allowIsolated);
}
SystemServiceManager
SystemServiceManager是Android系統(tǒng)中用于管理系統(tǒng)服務(wù)的一個類。負(fù)責(zé)管理所有注冊的系統(tǒng)級別服務(wù),并在需要時啟動和停止它們。例如,當(dāng)SystemServer進(jìn)程啟動時,SystemServiceManager會注冊PackageManagerService、WindowManagerService、ActivityManagerService等服務(wù),并在需要時啟動。
@SuppressWarnings("unchecked")
public <T extends SystemService> T startService(Class<T> serviceClass) {
final String name = serviceClass.getName();
Slog.i(TAG, "Starting " + name);
// Create the service.
if (!SystemService.class.isAssignableFrom(serviceClass)) {
throw new RuntimeException("Failed to create " + name
+ ": service must extend " + SystemService.class.getName());
}
final T service;
try {
Constructor<T> constructor = serviceClass.getConstructor(Context.class);
service = constructor.newInstance(mContext);
} catch (InstantiationException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service could not be instantiated", ex);
} catch (IllegalAccessException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service must have a public constructor with a Context argument", ex);
} catch (NoSuchMethodException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service must have a public constructor with a Context argument", ex);
} catch (InvocationTargetException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service constructor threw an exception", ex);
}
// Register it.
mServices.add(service);
// Start it.
try {
service.onStart();
} catch (RuntimeException ex) {
throw new RuntimeException("Failed to start service " + name
+ ": onStart threw an exception", ex);
}
return service;
}
SystemServiceManager通過調(diào)用registerService函數(shù)來注冊系統(tǒng)服務(wù),并可以通過startService、stopService函數(shù)來啟動或停止這些服務(wù)。當(dāng)系統(tǒng)不再需要某個服務(wù)時,SystemServiceManager也會負(fù)責(zé)將其停止并卸載。
SystemServiceManager是Android系統(tǒng)內(nèi)部使用的組件,通常不需要開發(fā)者直接與其交互。
SystemService
SystemService是framework的一些對應(yīng)功能的服務(wù),供其他模塊和APP來調(diào)用。這些服務(wù)通常與特定的功能或模塊相關(guān),例如BatteryService(用于獲取電池屬性、充電狀態(tài)、百分比等)、PowerManagerService(控制休眠、喚醒等)以及TvInputManagerService(用于創(chuàng)建和釋放會話等)。
SystemService的使用相對簡單,通過context.getSystemService(@NonNull Class<T> serviceClass)或Object getSystemService(@ServiceName @NonNull String name)方法獲取一個manager對象,然后調(diào)用SystemService里面的方法。
SystemService是Android系統(tǒng)內(nèi)部使用的組件,開發(fā)者在開發(fā)Android應(yīng)用程序時,通常是通過系統(tǒng)提供的API來與系統(tǒng)服務(wù)進(jìn)行交互的,而不是直接操作SystemService。
val powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager