Android Lollipop know if app as Usage Stats access

非 Y 不嫁゛ 提交于 2019-12-09 11:35:45

问题


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

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.

Thanks!


回答1:


you can simply query usagestats with daily interval and end time the current time and if nothing is returned this means the user hasn’t granted permissions

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


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

    return !queryUsageStats.isEmpty();
}

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.




回答2:


Check this answer: Tomik's answer

If you hurry, here's the solution ;)

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static boolean usageAccessGranted(Context context) {
       AppOpsManager appOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
       int mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
       android.os.Process.myUid(), context.getPackageName());
       return mode == AppOpsManager.MODE_ALLOWED;
    }



回答3:


I stumbled on the same problem. On Samsung S5 Lollipop usage stats did not work with the following code:

Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

However usage stats actually exist. With the following code one can open the security settings:

Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
intent.setComponent(new ComponentName("com.android.settings","com.android.settings.Settings$SecuritySettingsActivity"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

An the scroll to the bottom and there is usage stats. I also inspeced logs and by pressing usage stats, you are directed to SubActivity which contains UsageStats fragment. I tried the following code:

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.SubSettings"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

But got security exception. The problem is that they didnt mark SubActivity as exported, so as far as I know its not possible to directly start SubActivity (usage stats window). The only solution is to take user to Securiy settings and them tell him to manually go to usage stats view and enable app.

If someone finds better solution it would be great!




回答4:


See ActivityNotFoundException in Lollipop when trying to launch activity with intent android.settings.USAGE_ACCESS_SETTINGS for a better way, since with method we cannot discern whether there are simple no stats for the time interval



来源:https://stackoverflow.com/questions/27330339/android-lollipop-know-if-app-as-usage-stats-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!