日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android工作机制和内核,android内核剖析学习笔记:AMS(ActivityManagerService)内部原理和工作机制...

發布時間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)内部原理和工作机制...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。