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