Android 系統(tǒng)中比較重要的系統(tǒng)服務ActivityManagerService(AMS)和 WindowManagerService(WMS)陨享,PackageManagerService(PMS)葱淳。
定義
AMS:ActivityManagerService 負責管理Activity的生命周期的钝腺,AMS還借助ActivityStack是來把所有的Activity按照后進先出的順序放在一個堆棧中。在Android系統(tǒng)中只有一個AMS的實例赞厕,他負責管理系統(tǒng)中所有的Activity艳狐,管理、調度這些Activity的生命周期皿桑。在Android系統(tǒng)系統(tǒng)的過程中毫目,在SystemServer進程中把AMS服務啟動起來,注冊到ServiceManager中诲侮。
————————————————
WMS:WindowManagerService 負責管理系統(tǒng)中所有的窗口镀虐,包括Activity的窗口、壁紙窗口沟绪、輸入法窗口刮便、彈窗子窗口等,即管理屏幕上展示上的一切窗口绽慈。在Android系統(tǒng)系統(tǒng)的過程中恨旱,在SystemServer進程中也把WMS服務啟動起來,注冊到ServiceManager中坝疼。
————————————————
PMS:管理著所有跟 package 相關的工作搜贤,常見的比如安裝、卸載應用钝凶。
————————————————
先看一張圖-AMS和WMS涉及的類:
詳解AMS:
1).ActivityManagerServices仪芒,簡稱 AMS,服務端對象,負責系統(tǒng)中所有 Activity 的生命周期掂名。 2).ActivityThread夭咬,App 的真正入口。當開啟 App 之后铆隘,調用 main()開始運行卓舵, 開啟消息循環(huán)隊列,這就是傳說的 UI 線程或者叫主線程膀钠。與 ActivityManagerService 一起完成 Activity 的管理工作掏湾。
3).ApplicationThread,用來實現(xiàn) ActivityManagerServie 與 ActivityThread 之間的 交互肿嘲。在 ActivityManagerSevice 需要管理相關 Application 中的 Activity 的生命 周期時融击,通過 ApplicationThread 的代理對象與 ActivityThread 通信。
4).ApplicationThreadProxy雳窟,是 ApplicationThread 在服務器端的代理尊浪,負責和客 戶端的 ApplicationThread 通信。AMS 就是通過該代理與 ActivityThread 進行通 信的封救。 5).Instrumentation拇涤,每一個應用程序只有一個 Instrumetation 對象,每個 Activity 內都有一個對該對象的引用誉结,Instrumentation 可以理解為應用進程的管家鹅士, ActivityThread 要創(chuàng)建或暫停某個 Activity 時,都需要通過 Instrumentation 來進 行具體的操作惩坑。
6).ActivityStack掉盅,Activity 在 AMS 的棧管理,用來記錄經啟動的 Activity 的先后 關系以舒,狀態(tài)信息等趾痘。通過 ActivtyStack 決定是否需要啟動新的進程。
7).ActivityRecord蔓钟,ActivityStack 的管理對象永票,每個 Acivity 在 AMS 對應一個 ActivityRecord,來記錄 Activity 狀態(tài)以及其他的管理信息奋刽。其實就是服務器端的 Activit 對象的映像瓦侮。 8).TaskRecord,AMS 抽象出來的一個“任務”的概念佣谐,是記錄 ActivityRecord 的棧肚吏, 一個“Task”包含若干個 ActivityRecord。AMS 用 TaskRecord 確保 Activity 啟動和 退出的順序狭魂。如果你清楚 Activity 的 4 種 launchMode罚攀,那么對這概念應該不陌 生党觅。
[圖片上傳中...(image.png-c635d9-1619582584474-0)]
詳解WMS
1).WindowManagerService 負責管理系統(tǒng)中所有的窗口,包括Activity的窗口斋泄、壁紙窗口杯瞻、輸入法窗口、彈窗子窗口等炫掐,即管理屏幕上展示上的一切窗口魁莉。在Android系統(tǒng)系統(tǒng)的過程中,在SystemServer進程中也把WMS服務啟動起來募胃,注冊到ServiceManager中旗唁。
2).Session 官方解釋 This class represents an active client session. There is generally one Session object per process that is interacting with the window manager。應用程序進程通過Session對象與WMS進行通信痹束,每個應用程序進程中有一個Session對象检疫。
3).WindowState 官方解釋 A window in the window manager。在WMS中描述一個Window祷嘶。
4).WindowToken 官方解釋 Container of a set of related windows in the window manager. Often this is an AppWindowToken, which is the handle for an Activity that it uses to display windows. For nested windows, there is a WindowToken created for the parent window to manage its children.用來描述WMS中一個Window的信息屎媳。
5).AppWindowToken 官方解釋 Version of WindowToken that is specifically for a particular application (or really activity) that is displaying windows.用來描述一個Activity在WMS中對應的Window的信息。
6).SurfaceSession 官方解釋 An instance of this class represents a connection to the surface flinger, from which you can create one or more Surface instances that will be composited to the screen.通過該對象能與SurfaceFlinger服務通信论巍,來創(chuàng)建一個Surface.
————————————————
詳解PMS
APK 的安裝流程如下所示:
1).復制 APK 到/data/app 目錄下烛谊,解壓并掃描安裝包。
2).資源管理器解析 APK 里的資源文件环壤。
3).解析 AndroidManifest 文件晒来,并在/data/data/目錄下創(chuàng)建對應的應用數(shù)據(jù)目錄。
4).然后對 dex 文件進行優(yōu)化郑现,并保存在 dalvik-cache 目錄下。
5).將 AndroidManifest 文件解析出的四大組件信息注冊到 PackageManagerService 中荧降。
6).安裝完成后接箫,發(fā)送廣播。