检查我的应用程序使用权限启用 [英] Check if my application has usage access enabled

查看:257
本文介绍了检查我的应用程序使用权限启用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是新的 UsageStatsManager API来获取当前前台应用程序< /一>中的Andr​​oid 5.0棒棒糖。 为了使用这个API,用户必须启用设置 - &GT的应用;安全性 - &GT;有使用权限的应用屏幕

I'm using the new UsageStatsManager API to get current foreground application in Android 5.0 Lollipop. In order to use this API, the user must enable the application in the Settings->Security->Apps with usage access screen.

我直接将用户该屏幕与此意图:

I send the user directly to this screen with this Intent:


startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));

现在,我要验证用户启用了我的申请。 我想这样做,像我验证用户启用了我的应用程序使用 NotificationListenerService ,但我不知道什么是字符串键,如果它的存在。

Now, I want to validate the user enabled my application. I wanted to do so like I validate the user enabled my application to use the NotificationListenerService but I have no idea what is the String key, if it even exists.


Settings.Secure.getString(contentResolver, "enabled_notification_listeners");
// Tried Settings.ACTION_USAGE_ACCESS_SETTINGS as key but it returns null

第二条本办法是查询使用状态,并检查它是否返回结果(返回时,未启用应用程序一个空数组)和它的作品大部分时间,但有时它会返回0的结果,即使在我的应用程序启用。

Second approach was to query the usage stats and check if it returns results (it returns an empty array when the app is not enabled) and it works most of the times but sometimes it returns 0 results even when my app is enabled.


UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService("usagestats");
long time = System.currentTimeMillis();
List stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 10, time);

if (stats == null || stats.isEmpty()) {
    // Usage access is not enabled
}

有没有一种方法来检查,如果我的应用程序启用了使用权限?

Is there a way to check if my application has usage access enabled?

推荐答案

获得了极大的答案有人在Twitter上,测试的工作:

Received a great answer by someone on Twitter, tested working:

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 false;
}

这篇关于检查我的应用程序使用权限启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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