Android UsageStatsManager:获取手机上当前正在运行的应用程序的列表 [英] Android UsageStatsManager: Get a list of currently running apps on phone

查看:143
本文介绍了Android UsageStatsManager:获取手机上当前正在运行的应用程序的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取手机上当前正在运行的应用程序的列表.这是我正在使用的代码:

I am trying to get a list of currently running apps on my phone. Here is the code I am using:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            UsageStatsManager usm = (UsageStatsManager)this.getSystemService(Context.USAGE_STATS_SERVICE);
            long time = System.currentTimeMillis();
            List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,  time - 10000*10000, time);
            if (appList != null && appList.size() == 0) {
                Log.d("Executed app", "######### NO APP FOUND ##########" );
            }
            if (appList != null && appList.size() > 0) {
                SortedMap<Long, UsageStats> mySortedMap = new TreeMap<Long, UsageStats>();
                for (UsageStats usageStats : appList) {
                    Log.d("Executed app", "usage stats executed : " +usageStats.getPackageName() + "\t\t ID: ");
                    mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
                }
                if (mySortedMap != null && !mySortedMap.isEmpty()) {
                    String currentApp = mySortedMap.get(mySortedMap.lastKey()).getPackageName();

                }
            }
        }

在这里,我还设置了权限:

Here I have set the permission as well:

<uses-permission
    android:name="android.permission.PACKAGE_USAGE_STATS"
    tools:ignore="ProtectedPermissions" />

它不检测任何应用程序使用情况.我在stackoverflow上尝试了其他一些东西,但似乎没有任何效果.我什至在某处读到,在较新的手机中,应用程序监视手机上运行的其他应用程序的功能已被完全删除.将不胜感激任何帮助!

It does not detect any app usage. I have tried some other things on stackoverflow but nothing seems to work. I have even read somewhere that the ability for apps to monitor other apps running on the phone has been removed completely in the newer phones. Would greatly appreciate any help!

推荐答案

问题是您的应用程序没有 android:get_usage_stats 的系统权限.

The problem is that your application does not have the system permission of android:get_usage_stats.

您可以使用以下代码检查您是否具有权限:

You can use the below code to check if you have the permission:

public static boolean needPermissionForBlocking(Context context){
try {
    PackageManager packageManager = context.getPackageManager();
    ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
    AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
    int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid, applicationInfo.packageName);
    return  (mode != AppOpsManager.MODE_ALLOWED);
} catch (PackageManager.NameNotFoundException e) {
    return true;
}
}

如果您没有此权限,则用户必须启用此权限,方法是转到设置"->安全性"->具有使用权限的应用程序",然后添加您的应用程序.然后,您的代码应该可以正常工作.

If you do not have the permission then the user must enable this permission by going to Settings -> Security-> Apps with usage access, and then adding your application. Your code should then work fine.

这篇关于Android UsageStatsManager:获取手机上当前正在运行的应用程序的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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