ActivityManager服务是对Activity管理、运行时功能管理和运行时数据结构的封装,进程(Process)、应用程序/包、服务(Service)、任务(Task)信息等。包括以下功能:
常用内部静态类有:
ActivityManager.MemoryInfo: 系统可用内存信息
ActivityManager.RecentTaskInfo: 最近的任务信息
ActivityManager.RunningAppProcessInfo: 正在运行的进程信息
ActivityManager.RunningServiceInfo: 正在运行的服务信息
ActivityManager.RunningTaskInfo: 正在运行的任务信息
用法:
1.获取ActivityManager服务:
ActivityManager mActivityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
2.获取内存信息:
MemoryInfo memoryInfo = new MemoryInfo() ;
mActivityManager.getMemoryInfo(memoryInfo);
3.获取正在运行的task信息:
ListrunningTaskInfos = mActivityManager.getRunningTasks(Integer.MAX_VALUE); String cmpNameTemp = null; if (null != runningTaskInfos) { cmpNameTemp = (runningTaskInfos.get(0).topActivity).toString(); }
4.判断android应用是否在前台:
public static boolean isAppOnForeground(Context context) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); String packageName = context.getPackageName(); ListappTask = activityManager.getRecentTasks(Integer.MAX_VALUE, 1); if (appTask == null) { return false; } if (appTask.get(0).baseIntent.toString().contains(packageName)) { return true; } return false; }
5.获取android手机内安装的所有桌面:
private static ListgetAllTheLauncher(Context context) { List names = null; PackageManager pkgMgt = context.getPackageManager(); Intent it = new Intent(Intent.ACTION_MAIN); it.addCategory(Intent.CATEGORY_HOME); List ra = pkgMgt.queryIntentActivities(it, 0); if (ra.size() != 0) { names = new ArrayList (); } for (int i = 0; i < ra.size(); i++) { String packageName = ra.get(i).activityInfo.packageName; names.add(packageName); } return names; }
6.判断程序前后台状态:
public static boolean isLauncherRunning(Context context) { boolean result = false; Listnames = getAllTheLauncher(context); ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List appList = mActivityManager.getRunningAppProcesses(); for (RunningAppProcessInfo running : appList) { if (running.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { for (int i = 0; i < names.size(); i++) { if (names.get(i).equals(running.processName)) { result = true; break; } } } } return result; }
7.获取系统中进程信息:
private void getRunningAppProcessInfo() {
processInfoList = new ArrayList
List
for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessList) {
int pid = appProcessInfo.pid;
int uid = appProcessInfo.uid;
String processName = appProcessInfo.processName;
int[] myMempid = new int[] { pid };
Debug.MemoryInfo[] memoryInfo = mActivityManager.getProcessMemoryInfo(myMempid);
int memSize = memoryInfo[0].dalvikPrivateDirty;
Log.i(TAG, "processName: " + processName + " pid: " + pid
+ " uid:" + uid + " memorySize is -->" + memSize + "kb");
// 构造一个ProcessInfo对象
ProcessInfo processInfo = new ProcessInfo();
processInfo.setPid(pid);
processInfo.setUid(uid);
processInfo.setMemSize(memSize);
processInfo.setPocessName(processName);
processInfoList.add(processInfo);
// 获得每个进程里运行的应用程序(包),即每个应用程序的包名
String[] packageList = appProcessInfo.pkgList;
Log.i(TAG, "process id is " + pid + "has " + packageList.length);
for (String pkg : packageList) {
Log.i(TAG, "packageName " + pkg + " in process id is -->"+ pid);
}
}
}