/** * Android将进程分为6个等级,它们按优先级顺序由高到低依次是: 1) 前台进程( FOREGROUND_APP) 2) * 可视进程(VISIBLE_APP ) 3) 次要服务进程(SECONDARY_SERVER ) 4) 后台进程 (HIDDEN_APP) 5) * 内容供应节点(CONTENT_PROVIDER) 6) 空进程(EMPTY_APP)。注意:注意值越大说明进程重要程度越低。 */ public void ClearMemoryAction() { // final String TAG = "clearMemory"; ActivityManager am = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> runnningAppProcessInfo = am .getRunningAppProcesses(); // 获得正在运行的进程 if (runnningAppProcessInfo != null) { for (int i = 0; i < runnningAppProcessInfo.size(); ++i) { RunningAppProcessInfo appProcessInfo = runnningAppProcessInfo .get(i); // 一般数值大于RunningAppProcessInfo.IMPORTANCE_VISIBLE的进程都是非可见进程,也就是在后台运行着 if (appProcessInfo.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) { String[] packagesList = appProcessInfo.pkgList; // 运行在该进程下的所有应用程序包名,一个进程里可以运行一个或多个应用程序 for (int j = 0; j < packagesList.length; ++j) { am.killBackgroundProcesses(packagesList[j]); Log.i(TAG, "杀死的进程的包名" + packagesList[j]); } } } } } public int getProcessCount() { // final String TAG = "clearMemory"; ActivityManager am = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> runnningAppProcessInfo = am .getRunningAppProcesses(); // 获得正在运行的进程 int count = 0; if (runnningAppProcessInfo != null) { for (int i = 0; i < runnningAppProcessInfo.size(); ++i) { RunningAppProcessInfo appProcessInfo = runnningAppProcessInfo .get(i); // 一般数值大于RunningAppProcessInfo.IMPORTANCE_VISIBLE的进程都是非可见进程,也就是在后台运行着 if (appProcessInfo.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) { String[] packagesList = appProcessInfo.pkgList; // 运行在该进程下的所有应用程序包名,一个进程里可以运行一个或多个应用程序 for (int j = 0; j < packagesList.length; ++j) { count++; } } } } Log.i(TAG, "杀死的进程的数目" + count); return count; }
时间: 2024-10-09 13:06:16