android工作机制和内核,android内核剖析学习笔记:AMS(ActivityManagerService)内部原理和工作机制...
一、ActivityManagerService提供的主要功能:
(1)統一調度各應用程序的Activity
(2)內存管理
(3)進程管理
二、啟動一個Activity的方式有以下幾種:
(1)在應用程序中調用startActivity啟動指定的Activity
(2)在Home程序中單擊一個應用圖標,啟動新的Activity
(3)按“Back”鍵,結束當前Activity,返回到上一個Activity
(4)長按“Home”鍵,顯示出當前正在運行的程序列表,從中選擇一個啟動
這四種啟動方式的主體處理流程都會按照第一種啟動方式運行,后面三種方式只是在前端消息處理上各有不同。
三、進程數據類ProcessRecord
該類的源代碼在~\frameworks\base\services\java\com\android\server\am路徑下。
一般情況下,一個APK文件運行時會對應一個進程,ProcessRecord用來記錄一個進程中的相關信息,主要包含的變量有:
(1)進程文件信息:與該進程對應的APK文件的內部信息,如
final ApplicationInfo info; // all about the first app in the process
final String processName; ? // name of the process
final ArrayMap pkgList
= new ArrayMap(); ? //保存進程中所有APK文件包名
(2)進程的內存狀態信息:用于Linux系統的out of memory(OOM)情況的處理,當發生內存緊張時,Linux系統會根據進程的內存狀態信息殺掉低優先級的進程,包括的變量有
int maxAdj; ? ? ? ? ? ? ? ? // Maximum OOM adjustment for this process
int curRawAdj; ? ? ? ? ? ? ?// Current OOM unlimited adjustment for this process
int setRawAdj; ? ? ? ? ? ? ?// Last set OOM unlimited adjustment for this process
int curAdj; ? ? ? ? ? ? ? ? // Current OOM adjustment for this process
int setAdj; ? ? ? ? ? ? ? ? // Last set OOM adjustment for this process
變量中Adj的含義是調整值(adjustment)
(3)進程中包含的Activity、Provider、Service等,如下
final ArrayList activities = new ArrayList();
final ArraySet services = new ArraySet();
final ArraySet executingServices?= new ArraySet();
final ArraySet connections?= new ArraySet();
final ArraySet receivers = new ArraySet();
final ArrayMap
pubProviders?= new ArrayMap
ContentProviderRecord>();
final ArrayList conProviders?= new ArrayList();
四、ActivityRecord數據類(Android 2.3以前版本叫HistoryRecord類)
ActivityManagerService使用ActivityRecord數據類來保存每個Activity的信息,ActivityRecord類基于IApplicationToken.Stub類,也是一個Binder,所以可以被IPC調用。
主要包含的變量有:
(1)環境信息:Activity的工作環境,比如進程名稱、文件路徑、數據路徑、圖標、主題等,這些信息一般是固定的,比如以下變量
final String packageName; // the package implementing intent's component
final String processName; // process where this component wants to run
final String baseDir; ? // where activity source (resources etc) located
final String resDir; ? // where public activity source (public resources etc) located
final String dataDir; ? // where activity data should go
int theme; ? ? ? ? ? ? ?// resource identifier of activity's theme.
int realTheme; ? ? ? ? ?// actual theme resource we will use, never 0.
(2)運行狀態數據信息:如idle、stop、finishing等,一般為boolean類型,如下
boolean haveState; ? ? ?// have we gotten the last activity state?
boolean stopped; ? ? ? ?// is activity pause finished?
boolean delayedResume; ?// not yet resumed because of stopped app switches?
boolean finishing; ? ? ?// activity in pending finish list?
boolean configDestroy; ?// need to destroy due to config change?
五、TaskRecord類
ActivityManagerService中使用任務的概念來確保Activity啟動和退出的順序。
TaskRecord中的幾個重要變量如下:
final int taskId; ? ? ? // 每個任務的標識.
Intent intent; ? ? ? ? ?// 創建該任務時對應的intent
int numActivities; ? //該任務中的Activity數目
final ArrayList mActivities = new ArrayList(); ?//按照出現的先后順序列出該任務中的所有Activity
六、ActivityManagerService中一些重要的與調度相關的變量
(1)記錄最近啟動的Activity,如果RAM容量較小,則記錄的最大值為10個,否則為20個,超過該值后,Ams會舍棄最早記錄的Activity
static final int MAX_RECENT_TASKS = ActivityManager.isLowRamDeviceStatic() ? 10 : 20;
(2)當Ams通知應用程序啟動(Launch)某個Activity時,如果超過10s,Ams就會放棄
static final int PROC_START_TIMEOUT = 10*1000;
(3)當Ams啟動某個客戶進程后,客戶進程必須在10s之內報告Ams自己已經啟動,否則Ams會認為指定的客戶進程不存在
static final int PROC_START_TIMEOUT = 10*1000;
(4)等待序列:
當Ams內部還沒有準備好時,如果客戶進程請求啟動某個Activity,那么會被暫時保存到該變量中,
final ArrayList mPendingActivityLaunches
= new ArrayList();
(5)優先啟動,其次再停止。進程A1包含兩個Activity,啟動順序為A1->A2,當用戶請求啟動A2時,如果A1正在運行,Ams會先暫停A1,然后啟動A2,當A2啟動后再停止A1。
private final ArrayList mRecentTasks = new ArrayList();
七、startActivity()的流程
當用戶單擊某個應用圖標后,執行程序會在該圖標的onClick()事件中調用startActivity()方法,該方法會調用
startActivityForResult(),在這個方法內部會調用Instrumentation對象的
executeStartActivity()方法,每個Activity內部都有一個Instrumentation對象的引用,它就是一個管
家,ActivityThread要創建或者暫停某個Activity都是通過它實現的。
流程圖如下所示:
下面附上ActivityManagerService的完整源代碼,有興趣的童鞋可以深入研究。
總結
以上是生活随笔為你收集整理的android工作机制和内核,android内核剖析学习笔记:AMS(ActivityManagerService)内部原理和工作机制...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大数据 数据库 评测_中国信通院公布第九
- 下一篇: java后台管理 开源_12款开源的JA