Many API s are encapsulated in the ActivityManager class of the Java layer in Android, which allows us to query a lot of information about the current system, including memory, process, task, service and so on.
Using this information, you can make some useful judgments, such as whether the current system memory is insufficient and whether the specified Service is running.
(the ActivityManager class encapsulates many API methods for the upper layer to call. Specifically, it is responsible for managing activities, services and other components
Activity manager service (AMS), and most of these functions are implemented in the native layer.
)
1. Get memory information
//Get memory information
//MemoryInfo is an important attribute
//availMem: currently available memory of the system
//totalMem: total memory of the system
//Threshold: the threshold at which the system determines that it is out of memory, that is, the critical value. Below this value, the system will start to kill some lower priority services and processes.
//lowMemory: whether it is currently in the second memory state. If the currently available memory is < = threshold, lowMemory is true
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo(); mActivityManager.getMemoryInfo(memoryInfo);
2. Get the running process information
//Get running process information
//RunningAppProcessInfo important properties
//importance: the priority of the process in the system. The lower the value, the less likely the process is to be killed
//importanceReasonCode: the important reason code of the process
//importanceReasonComponent: the description of the component in the process
//pkgList: all package names loaded into the current process
//processName: the name of the current process
The following are the common priority corresponding values for Android. The lower the value, the higher the corresponding priority, and the less likely the process is to be killed by the system:
* ActivityManager.RunningAppProcessInfo.
* IMPORTANCE_ Foreround = 100, running the foreground component (Interactive), or binding the Service of the foreground component
* IMPORTANCE_FOREGROUND_SERVICE=125, running the foreground service
* IMPORTANCE_VISIBLE=200, visible process (there are components visible and non interactive, or there are services bound to such components)
* IMPORTANCE_SERVICE=300,
* IMPORTANCE_TOP_SLEEPING=325,
* IMPORTANCE_CACHED=400,
* IMPORTANCE_EMPTY=500,
* IMPORTANCE_GONE=1000
* ActivityManager.RunningAppProcessInfo.
* IMPORTANCE_ Foreround = 100, running the foreground component (Interactive), or binding the Service of the foreground component
* IMPORTANCE_FOREGROUND_SERVICE=125, running the foreground service
* IMPORTANCE_VISIBLE=200, visible process (there are components visible and non interactive, or there are services bound to such components)
* IMPORTANCE_SERVICE=300,
* IMPORTANCE_TOP_SLEEPING=325,
* IMPORTANCE_CACHED=400,
* IMPORTANCE_EMPTY=500,
* IMPORTANCE_GONE=1000
List<ActivityManager.RunningAppProcessInfo> processInfoList = mActivityManager.getRunningAppProcesses();
3. Get the running service information
//Get running service information
//In the official document, it is recommended to use this API only when debug ging, and there should be no important code logic based on the results of this API
//RunningServiceInfo property
//activeSince: the time when the service is activated for the first time (startup and binding mode)
//foreground: is it a front desk service
//lastActivityTime: the time when the last Activity is bound to the service
//Service: the component name of the service
//started: if this value is true, it indicates that the service is already starting and running
List<ActivityManager.RunningServiceInfo> serviceInfoList = mActivityManager.getRunningServices(Integer.MAX_VALUE);
4. Get the running task stack information
//Get the running task stack information
//In the official document, it is recommended to use this API only when debug ging, and there should be no important code logic based on the results of this API
//Permission required: < uses permission Android: name = "Android. Permission. Get_tasks" / >
//RunningTaskInfo important properties
//baseActivity: Activity at the bottom of the stack
//topActivity: stack top Activity
//numActivities: the total number of activities in the stack, including those that have been stopped
//numRunning, the number of running activities in the task, excluding those that have been stopped
List<ActivityManager.RunningTaskInfo> taskInfoList = mActivityManager.getRunningTasks(Integer.MAX_VALUE);
#Some other API s
getDeviceConfigurationInfo(): get device information
getProcessMemoryInfo (int[] pids): returns the current memory usage information of one or more processes
Kill backgroundprocesses (string packagename): kill the corresponding process according to the package name
clearApplicationUserData(): clearing the cached data of the current App in the internal storage space is equivalent to clearing the user data in the mobile phone settings.
getAppTasks(): get the task stack list related to the current application
Addapptask (activity, intent, intent, activitymanager.taskdescription, description, bitmap thumbnail): create a new task stack for the activity, activity (the activity that needs to create the task stack), intent (the intent used to jump the page), description (description), thumbnail (thumbnail)
getLauncherLargeIconSize(): get the size of the Launcher icon
isUserAMonkey(): whether the user clicks as fast as a monkey
##. Application example
/** * Gets the priority of the process corresponding to the specified APP package name * APP The process corresponding to the package name is its default process name, which is generally the most important process. And most apps have only one process. * The following are the common priority corresponding values for Android. The lower the value, the higher the corresponding priority, and the less likely the process is to be killed by the system: * ActivityManager.RunningAppProcessInfo. * IMPORTANCE_FOREGROUND=100, Running the foreground component (Interactive), or binding the Service of the foreground component * IMPORTANCE_FOREGROUND_SERVICE=125, Running the foreground Service * IMPORTANCE_VISIBLE=200, Visible process (there are components that are visible and non interactive, or there are services that bind such components) * IMPORTANCE_SERVICE=300, * IMPORTANCE_TOP_SLEEPING=325, * IMPORTANCE_CACHED=400, * IMPORTANCE_EMPTY=500, * IMPORTANCE_GONE=1000 * @param packageName * @return */ public int getAppProcessImportance(Context context, String packageName){ if(context == null || TextUtils.isEmpty(packageName)){ return Integer.MAX_VALUE; } init(context); List<ActivityManager.RunningAppProcessInfo> processList = mActivityManager.getRunningAppProcesses(); if(processList == null || processList.size() == 0){ return Integer.MAX_VALUE; } for(ActivityManager.RunningAppProcessInfo processInfo : processList){ if(processInfo == null || TextUtils.isEmpty(processInfo.processName)){ continue; } if(processInfo.processName.equals(packageName)){ return processInfo.importance; } } return Integer.MAX_VALUE; }