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:
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);
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?