下面是我這個獲取所有運行的程序的方法,為什麼在5.0系統以上中卻獲取不到呢?有這方面了解的前輩麼?請求指教!
/**
* 獲取所有正在運行的程序
* @param context
* @return
*/
public static List<Programe> getRunningProcess(Context context) {
final ProgrameUtils proutils = new ProgrameUtils(context);
final List<Programe> list = new ArrayList<Programe>();
PackageManager pm = context.getPackageManager();
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> infoList = am.getRunningAppProcesses();
if (infoList != null) {
for (int i = 0; i < infoList.size(); ++i) {
RunningAppProcessInfo appProcessInfo = infoList.get(i);
ApplicationInfo app = proutils.getApplicationInfo(appProcessInfo.processName);
//過濾自己的應用
if (app == null || context.getPackageName().equals(app.packageName)) {
continue;
}
//過濾系統的應用
if ((app.flags & app.FLAG_SYSTEM) > 0) {
continue;
}
Programe pr = new Programe();
pr.setAppProcessInfo(appProcessInfo);
pr.setProgrameIcon(app.loadIcon(pm));// 保存程序圖標
pr.setProgrameName(app.loadLabel(pm).toString());// 保存程序名字
pr.setPackageName(app.packageName);// 保存程序包名
// 計算應用所占內存大小
int[] myMempid = new int[] { appProcessInfo.pid };
Debug.MemoryInfo[] memoryInfo = am
.getProcessMemoryInfo(myMempid);
double memSize = memoryInfo[0].dalvikPrivateDirty / 1024.0;
int temp = (int) (memSize * 100);
memSize = temp / 100.0;
pr.setProgrameSize(memSize);// 保存程序所占內存大小
list.add(pr);
}
}
return list;
}
http://biancheng.dnbcw.info/shouji/344550.html