Article Title, article title Daquan
Android 5.0 obtains the latest application
The security ActivityManager. getRunningTasks () has been deprecated on 5.0.
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 "";
}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.