Android的棒棒糖知道,如果应用程序的使用情况统计访问 [英] Android Lollipop know if app as Usage Stats access

查看:321
本文介绍了Android的棒棒糖知道,如果应用程序的使用情况统计访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于Android棒棒糖,我们现在访问的应用程序使用统计信息的API。然而,你的应用程序必须由用户被授予这些权限。

Since Android Lollipop, we have now an API for accessing apps usage stats. However, your app must be granted those permissions by the user.

我知道,将用户重定向到使用Settings.ACTION_USAGE_ACCESS_SETTINGS这些设置。

I know that for redirecting the user to those settings using Settings.ACTION_USAGE_ACCESS_SETTINGS.

现在,我的问题是你怎么知道用户已授予您的权限,这样就可以阻止他重定向的设置。

Now, my question is how do you know the user has granted you those permissions so that you can stop redirecting him to the settings.

谢谢!

推荐答案

您可以简单地查询usagestats每天的间隔时间和结束时间当前的时间,如果没有什么回报,这意味着用户处理不当授予的权限

you can simply query usagestats with daily interval and end time the current time and if nothing is returns this means the user hasnt granted permissions

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public boolean doIHavePermission(){


    final UsageStatsManager usageStatsManager = (UsageStatsManager) context.getSystemService("usagestats");
    final List<UsageStats> queryUsageStats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, 0,  System.currentTimeMillis());

    return !queryUsageStats.isEmpty();
}

与开始日期0和结束日期每日间隔当前的时间必须至少回今天usage.So它将是空的只有未授予权限

Daily interval with start date 0 and end date the current time must at least return todays usage.So it will be empty only if permissions are not granted

这篇关于Android的棒棒糖知道,如果应用程序的使用情况统计访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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