看来谷歌终于关闭了获取当前前台应用程序包的所有大门。
棒棒糖更新后,杀死了getRunningTasks(int maxNum) http://developer.android.com/intl/zh-cn/reference/android/app/ActivityManager.html#getRunningTasks(int)并感谢这个答案 https://stackoverflow.com/a/28277427/444324,我使用这段代码来获取 Lollipop 以来的前台应用程序包:
final int PROCESS_STATE_TOP = 2;
RunningAppProcessInfo currentInfo = null;
Field field = null;
try {
field = RunningAppProcessInfo.class.getDeclaredField("processState");
} catch (Exception ignored) {
}
ActivityManager am = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appList = am.getRunningAppProcesses();
for (RunningAppProcessInfo app : appList) {
if (app.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND &&
app.importanceReasonCode == 0 ) {
Integer state = null;
try {
state = field.getInt( app );
} catch (Exception ignored) {
}
if (state != null && state == PROCESS_STATE_TOP) {
currentInfo = app;
break;
}
}
}
return currentInfo;
Android 5.1.1 及更高版本(6.0 Marshmallow)似乎已被杀死getRunningAppProcesses() http://developer.android.com/intl/zh-cn/reference/android/app/ActivityManager.html#getRunningAppProcesses()以及。它现在返回您自己的应用程序包的列表。
使用情况统计管理器
我们可以使用新的UsageStatsManager
API as 此处描述 https://stackoverflow.com/a/26885469/444324但它并不适用于所有应用程序。一些系统应用程序会返回相同的包
com.google.android.googlequicksearchbox
无障碍服务(2017 年 12 月:将被 Google 禁止使用)
一些应用程序使用AccessibilityService
(as 在这里看到 https://stackoverflow.com/a/27642535/444324)但它有一些缺点。
还有其他方法获取当前正在运行的应用程序包吗?
要获取 Android 1.6 - Android 6.0 上正在运行的进程列表,您可以使用我编写的这个库:https://github.com/jaredrummler/AndroidProcesses https://github.com/jaredrummler/AndroidProcesses该库读取 /proc 来获取进程信息。
Google 在 Android Nougat 中严格限制了对 /proc 的访问。要获取 Android Nougat 上正在运行的进程列表,您需要使用UsageStatsManager 或具有 root 访问权限。
单击先前替代解决方案的编辑历史记录。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)