标签:android-l
android 5.0 获取最近运行的应用
由于考虑到安全性ActivityManager.getRunningTasks(),在5.0上已经deprecated了。
private String getLollipopRecentTask() {
final int PROCESS_STATE_TOP = 2;
try {
Field processStateField = ActivityManager.RunningAppProcessInfo.class.getDeclaredField("processState");
List<ActivityManager.RunningAppProcessInfo> processes = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE)).getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo process : processes) {
if (process.importance <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && process.importanceReasonCode == 0) {
int state = processStateField.getInt(process);
if (state == PROCESS_STATE_TOP) {
String[] packname = process.pkgList;
return packname[0];
}
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return "";
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:android-l
原文地址:http://blog.csdn.net/cqm1990/article/details/46983573