因為沒有系統分析過ActivityManagerService,簡單看來一點source code, 所以就簡單記錄在此,大概有謬誤,不做實際應用參考。
注:分析基於Android 9.0
一、進程狀態 Process states
參考源碼:http://aosp.opersys.com/xref/android-9.0.0_r61/xref/frameworks/base/core/java/android/app/ActivityManager.java#470
定義:進程狀態,描述特定進程所處的狀態類型。
各個狀態的定義如下:

1 /** 2 * @hide 3 * Process states, describing the kind of state a particular process is in. 4 * When updating these, make sure to also check all related references to the 5 * constant in code, and update these arrays: 6 * 7 * @see com.android.internal.app.procstats.ProcessState#PROCESS_STATE_TO_STATE 8 * @see com.android.server.am.ProcessList#sProcStateToProcMem 9 * @see com.android.server.am.ProcessList#sFirstAwakePssTimes 10 * @see com.android.server.am.ProcessList#sSameAwakePssTimes 11 * @see com.android.server.am.ProcessList#sTestFirstPssTimes 12 * @see com.android.server.am.ProcessList#sTestSamePssTimes 13 */ 14 15 /** @hide Not a real process state. */ 16 public static final int PROCESS_STATE_UNKNOWN = -1; //未知狀態,不是真正的進程狀態 17 18 /** @hide Process is a persistent system process. */ 19 public static final int PROCESS_STATE_PERSISTENT = 0; //常駐的系統進程 20 21 /** @hide Process is a persistent system process and is doing UI. */ 22 public static final int PROCESS_STATE_PERSISTENT_UI = 1; //進程是一個持久的系統進程,正在執行UI 23 24 /** @hide Process is hosting the current top activities. Note that this covers 25 * all activities that are visible to the user. */ 26 public static final int PROCESS_STATE_TOP = 2; //進程正在持有當前的top activities。請注意,這涵蓋了對用戶可見的所有活動。 27 28 /** @hide Process is hosting a foreground service. */ 29 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 3; //進程持有前台服務 30 31 /** @hide Process is hosting a foreground service due to a system binding. */ 32 public static final int PROCESS_STATE_BOUND_FOREGROUND_SERVICE = 4; //由於系統綁定,進程正在持有前台服務 33 34 /** @hide Process is important to the user, and something they are aware of. */ 35 public static final int PROCESS_STATE_IMPORTANT_FOREGROUND = 5; //進程對用戶來說很重要,而且是他們知道的 36 37 /** @hide Process is important to the user, but not something they are aware of. */ 38 public static final int PROCESS_STATE_IMPORTANT_BACKGROUND = 6; //進程對用戶來說很重要,但不是他們知道的。 39 40 /** @hide Process is in the background transient so we will try to keep running. */ 41 public static final int PROCESS_STATE_TRANSIENT_BACKGROUND = 7; //進程處於后台瞬態,因此我們將嘗試繼續運行。 42 43 /** @hide Process is in the background running a backup/restore operation. */ 44 public static final int PROCESS_STATE_BACKUP = 8; //進程正在后台運行備份/還原操作。 45 46 /** @hide Process is in the background running a service. Unlike oom_adj, this level 47 * is used for both the normal running in background state and the executing 48 * operations state. */ 49 public static final int PROCESS_STATE_SERVICE = 9; //進程在后台運行服務。與oom_adj不同的是,此級別同時用於正常的后台運行狀態和正在執行的操作狀態。 50 51 /** @hide Process is in the background running a receiver. Note that from the 52 * perspective of oom_adj, receivers run at a higher foreground level, but for our 53 * prioritization here that is not necessary and putting them below services means 54 * many fewer changes in some process states as they receive broadcasts. */ 55 public static final int PROCESS_STATE_RECEIVER = 10; //進程在后台運行一個接收器。 56 57 /** @hide Same as {@link #PROCESS_STATE_TOP} but while device is sleeping. */ 58 public static final int PROCESS_STATE_TOP_SLEEPING = 11; //與PROCESS _STATE _TOP相同,但設備處於睡眠狀態 59 60 /** @hide Process is in the background, but it can't restore its state so we want 61 * to try to avoid killing it. */ 62 public static final int PROCESS_STATE_HEAVY_WEIGHT = 12; //進程在后台,但它無法恢復其狀態,因此我們希望嘗試避免殺死它。 63 64 /** @hide Process is in the background but hosts the home activity. */ 65 public static final int PROCESS_STATE_HOME = 13; //進程在后台,但持有home活動 66 67 /** @hide Process is in the background but hosts the last shown activity. */ 68 public static final int PROCESS_STATE_LAST_ACTIVITY = 14; //進程位於后台,但承載最后顯示的活動。 69 70 /** @hide Process is being cached for later use and contains activities. */ 71 public static final int PROCESS_STATE_CACHED_ACTIVITY = 15; //進程正在緩存以供以后使用,並且包含活動。 72 73 /** @hide Process is being cached for later use and is a client of another cached 74 * process that contains activities. */ 75 public static final int PROCESS_STATE_CACHED_ACTIVITY_CLIENT = 16; //進程正在緩存以供以后使用,並且是另一個包含活動的緩存進程的客戶端。 76 77 /** @hide Process is being cached for later use and has an activity that corresponds 78 * to an existing recent task. */ 79 public static final int PROCESS_STATE_CACHED_RECENT = 17; //進程正在緩存以供以后使用,並且有一個活動對應現有的最近任務 80 81 /** @hide Process is being cached for later use and is empty. */ 82 public static final int PROCESS_STATE_CACHED_EMPTY = 18; //進程正在緩存以供以后使用,並且為空。 83 84 /** @hide Process does not exist. */ 85 public static final int PROCESS_STATE_NONEXISTENT = 19; //進程不存在
二、將進程狀態轉換為相應的IMPORTANCE_*常量

1 /** 2 * Constant for {@link #importance}: This process is running the 3 * foreground UI; that is, it is the thing currently at the top of the screen 4 * that the user is interacting with. 5 */ 6 public static final int IMPORTANCE_FOREGROUND = 100; 7 8 /** 9 * Constant for {@link #importance}: This process is running a foreground 10 * service, for example to perform music playback even while the user is 11 * not immediately in the app. This generally indicates that the process 12 * is doing something the user actively cares about. 13 */ 14 public static final int IMPORTANCE_FOREGROUND_SERVICE = 125; 15 16 /** 17 * @deprecated Pre-{@link android.os.Build.VERSION_CODES#P} version of 18 * {@link #IMPORTANCE_TOP_SLEEPING}. As of Android 19 * {@link android.os.Build.VERSION_CODES#P}, this is considered much less 20 * important since we want to reduce what apps can do when the screen is off. 21 */ 22 @Deprecated 23 public static final int IMPORTANCE_TOP_SLEEPING_PRE_28 = 150; 24 25 /** 26 * Constant for {@link #importance}: This process is running something 27 * that is actively visible to the user, though not in the immediate 28 * foreground. This may be running a window that is behind the current 29 * foreground (so paused and with its state saved, not interacting with 30 * the user, but visible to them to some degree); it may also be running 31 * other services under the system's control that it inconsiders important. 32 */ 33 public static final int IMPORTANCE_VISIBLE = 200; 34 35 /** 36 * Constant for {@link #importance}: {@link #IMPORTANCE_PERCEPTIBLE} had this wrong value 37 * before {@link Build.VERSION_CODES#O}. Since the {@link Build.VERSION_CODES#O} SDK, 38 * the value of {@link #IMPORTANCE_PERCEPTIBLE} has been fixed. 39 * 40 * <p>The system will return this value instead of {@link #IMPORTANCE_PERCEPTIBLE} 41 * on Android versions below {@link Build.VERSION_CODES#O}. 42 * 43 * <p>On Android version {@link Build.VERSION_CODES#O} and later, this value will still be 44 * returned for apps with the target API level below {@link Build.VERSION_CODES#O}. 45 * For apps targeting version {@link Build.VERSION_CODES#O} and later, 46 * the correct value {@link #IMPORTANCE_PERCEPTIBLE} will be returned. 47 */ 48 public static final int IMPORTANCE_PERCEPTIBLE_PRE_26 = 130; 49 50 /** 51 * Constant for {@link #importance}: This process is not something the user 52 * is directly aware of, but is otherwise perceptible to them to some degree. 53 */ 54 public static final int IMPORTANCE_PERCEPTIBLE = 230; 55 56 /** 57 * Constant for {@link #importance}: {@link #IMPORTANCE_CANT_SAVE_STATE} had 58 * this wrong value 59 * before {@link Build.VERSION_CODES#O}. Since the {@link Build.VERSION_CODES#O} SDK, 60 * the value of {@link #IMPORTANCE_CANT_SAVE_STATE} has been fixed. 61 * 62 * <p>The system will return this value instead of {@link #IMPORTANCE_CANT_SAVE_STATE} 63 * on Android versions below {@link Build.VERSION_CODES#O}. 64 * 65 * <p>On Android version {@link Build.VERSION_CODES#O} after, this value will still be 66 * returned for apps with the target API level below {@link Build.VERSION_CODES#O}. 67 * For apps targeting version {@link Build.VERSION_CODES#O} and later, 68 * the correct value {@link #IMPORTANCE_CANT_SAVE_STATE} will be returned. 69 * 70 * @hide 71 */ 72 public static final int IMPORTANCE_CANT_SAVE_STATE_PRE_26 = 170; 73 74 /** 75 * Constant for {@link #importance}: This process is contains services 76 * that should remain running. These are background services apps have 77 * started, not something the user is aware of, so they may be killed by 78 * the system relatively freely (though it is generally desired that they 79 * stay running as long as they want to). 80 */ 81 public static final int IMPORTANCE_SERVICE = 300; 82 83 /** 84 * Constant for {@link #importance}: This process is running the foreground 85 * UI, but the device is asleep so it is not visible to the user. Though the 86 * system will try hard to keep its process from being killed, in all other 87 * ways we consider it a kind of cached process, with the limitations that go 88 * along with that state: network access, running background services, etc. 89 */ 90 public static final int IMPORTANCE_TOP_SLEEPING = 325; 91 92 /** 93 * Constant for {@link #importance}: This process is running an 94 * application that can not save its state, and thus can't be killed 95 * while in the background. This will be used with apps that have 96 * {@link android.R.attr#cantSaveState} set on their application tag. 97 */ 98 public static final int IMPORTANCE_CANT_SAVE_STATE = 350; 99 100 /** 101 * Constant for {@link #importance}: This process process contains 102 * cached code that is expendable, not actively running any app components 103 * we care about. 104 */ 105 public static final int IMPORTANCE_CACHED = 400; 106 107 /** 108 * @deprecated Renamed to {@link #IMPORTANCE_CACHED}. 109 */ 110 public static final int IMPORTANCE_BACKGROUND = IMPORTANCE_CACHED; 111 112 /** 113 * Constant for {@link #importance}: This process is empty of any 114 * actively running code. 115 * @deprecated This value is no longer reported, use {@link #IMPORTANCE_CACHED} instead. 116 */ 117 @Deprecated 118 public static final int IMPORTANCE_EMPTY = 500; 119 120 /** 121 * Constant for {@link #importance}: This process does not exist. 122 */ 123 public static final int IMPORTANCE_GONE = 1000; 124 125 /** 126 * Convert a proc state to the correspondent IMPORTANCE_* constant. If the return value 127 * will be passed to a client, use {@link #procStateToImportanceForClient}. 128 * @hide 129 */ 130 public static @Importance int procStateToImportance(int procState) { 131 if (procState == PROCESS_STATE_NONEXISTENT) { 132 return IMPORTANCE_GONE; 133 } else if (procState >= PROCESS_STATE_HOME) { 134 return IMPORTANCE_CACHED; 135 } else if (procState == PROCESS_STATE_HEAVY_WEIGHT) { 136 return IMPORTANCE_CANT_SAVE_STATE; 137 } else if (procState >= PROCESS_STATE_TOP_SLEEPING) { 138 return IMPORTANCE_TOP_SLEEPING; 139 } else if (procState >= PROCESS_STATE_SERVICE) { 140 return IMPORTANCE_SERVICE; 141 } else if (procState >= PROCESS_STATE_TRANSIENT_BACKGROUND) { 142 return IMPORTANCE_PERCEPTIBLE; 143 } else if (procState >= PROCESS_STATE_IMPORTANT_FOREGROUND) { 144 return IMPORTANCE_VISIBLE; 145 } else if (procState >= PROCESS_STATE_FOREGROUND_SERVICE) { 146 return IMPORTANCE_FOREGROUND_SERVICE; 147 } else { 148 return IMPORTANCE_FOREGROUND; 149 } 150 }
三、監測進程狀態
在maybeUpdateUsageStatsLocked 函數中做監測
ProcessRecord app; app.setProcState ==> 原來的狀態 app.curProcState ==> 當前要轉入的狀態
比如,監測一個app process是否運行在foreground,可以加入如下patch
if(Arrays.asList(app.getPackageList()).contains("app package name")) { Slog.d("roger", "Checking proc [" + Arrays.toString(app.getPackageList()) + "] state changes: old = " + app.setProcState + ", new = " + app.curProcState); if(app.setProcState == ActivityManager.PROCESS_STATE_TOP && app.curProcState != ActivityManager.PROCESS_STATE_TOP) { Slog.d("roger", "your apk process state change from TOP to others"); } }