Stack Overflow archive
1 scoreaccepted

Is it possible using adb via root to grant the permission of PACKAGE_USAGE_STATS?

score
1
question views
4.7K
license
CC BY-SA 4.0

Using adb shell pm grant [PACKAGE_NAME] android.permission.PACKAGE_USAGE_STATS worked for me on several devices.

Make sure you added the following to the AndroidManifest:

xml
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"
  tools:ignore="ProtectedPermissions"/>

I was also able to use root to grant the permission. Again, the app must declare the permission in the AndroidManifest.

You can use AndroidShell to run the command as root. Example:

java
String packageName = context.getPackageName();
CommandResult result = Shell.SU.run("pm grant " + packageName + " android.permission.PACKAGE_USAGE_STATS");

As mentioned by Sagar, you can also ask the user to grant the permission in the settings app, which is Google's recommended way to do this:

java
startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));

Originally posted on Stack Overflow. Public user contributions are licensed under Creative Commons Attribution-ShareAlike.