Android 5.1.1 及更高版本 - getRunningAppProcesses() 仅返回我的应用程序包 [英] Android 5.1.1 and above - getRunningAppProcesses() returns my application package only

查看:67
本文介绍了Android 5.1.1 及更高版本 - getRunningAppProcesses() 仅返回我的应用程序包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎 Google 终于关闭了获取当前前台应用程序包的所有大门.

It seems Google finally closed all doors for getting the current foreground application package.

棒棒糖更新后,它杀死了getRunningTasks(int maxNum) 并感谢 这个答案,我使用了此代码用于获取自 Lollipop 以来的前台应用程序包:

After the Lollipop update, which killed getRunningTasks(int maxNum) and thanks to this answer, I used this code to get the foreground application package since 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() 也是如此.它现在返回您自己的应用程序包的列表.

Android 5.1.1 and above (6.0 Marshmallow), it seems, killed getRunningAppProcesses() as well. It now returns a list of your own application package.

UsageStatsManager

我们可以使用新的 UsageStatsManager API 作为此处描述的,但它不起作用适用于所有应用程序.一些系统应用会返回相同的包

We can use the new UsageStatsManager API as described here but it doesn't work for all applications. Some system applications will return the same package

com.google.android.googlequicksearchbox

<小时>

AccessibilityService(2017 年 12 月:将被 Google 禁止使用)


AccessibilityService (December 2017: Going to be banned for use by Google)

某些应用程序使用 AccessibilityService(如此处所示),但它有一些缺点.

Some applications use AccessibilityService (as seen here) but it has some disadvantages.

是否有其他方法可以获取当前正在运行的应用程序包?

推荐答案

要获取 Android 1.6 - Android 6.0 上正在运行的进程列表,您可以使用我写的这个库:https://github.com/jaredrummler/AndroidProcesses 该库读取/proc 以获取进程信息.

To get a list of running processes on Android 1.6 - Android 6.0 you can use this library I wrote: https://github.com/jaredrummler/AndroidProcesses The library reads /proc to get process info.

Google 严重限制了对 Android Nougat 中/proc 的访问.要获取 Android Nougat 上正在运行的进程列表,您需要使用 UsageStatsManager 或具有 root 访问权限.

Google has significantly restricted access to /proc in Android Nougat. To get a list of running processes on Android Nougat you will need to use UsageStatsManager or have root access.

单击以前替代解决方案的编辑历史记录.

Click the edit history for previous alternative solutions.

这篇关于Android 5.1.1 及更高版本 - getRunningAppProcesses() 仅返回我的应用程序包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆