6

I'm building an app for gathering and displaying detailed usage statistics (for science!) that uses the android.app.usage.UsageStatsManager system service. This service requires the system-level permission android.permission.PACKAGE_USAGE_STATS.

The user needs to explicitly grant system-level permissions under Settings. When the app is launched, I want to programmatically determine if the app has been already been granted this permission. If not, I want to display a prompt like this:

Permission prompt

To check if an app has a required permission, I would usually do something like this:

if (PackageManager.PERMISSION_DENIED == checkCallingOrSelfPermission("android.permission.PACKAGE_USAGE_STATS")) {
    // display prompt
}

// keep going

The prompt launches the Settings.ACTION_USAGE_ACCESS_SETTINGS intent:

final Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivity(intent);

App clearly has permission

However, the call to checkCallingOrSelfPermission(String) consistently returns PackageManager.PERMISSION_DENIED (a.k.a -1) even when the app already has been granted this permission.

Is there a special way of checking for system-level permissions, or am I doing something else wrong?

Martin Lehmann
  • 774
  • 8
  • 15
  • This question has been answered here I think : http://stackoverflow.com/questions/7203668/how-permission-can-be-checked-at-runtime-without-throwing-securityexception – tchoum Jul 02 '15 at 13:23
  • @Martin Lehmann Exactly, the answer in the above-mentioned does not work in this case. However, did you come up with a solution – 7geeky Aug 31 '16 at 12:06
  • @7geeky I did not! I abandoned this project shortly after writing this question, terribly sorry… – Martin Lehmann Aug 31 '16 at 19:32
  • @MartinLehmann thanks man. I think this [link](http://stackoverflow.com/a/28921586/2900127) would be helpful for any future use – 7geeky Sep 01 '16 at 01:41

0 Answers0