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