问题
I am trying to give BATTERSTATS permission to an app. When I run the command from a PC - it works just fine:
adb shell pm grant com.example.sample.myapplication android.permission.BATTERY_STATS
But the same pm grant command does not work when run from Android app:
java.lang.Process process = Runtime.getRuntime().exec(\"pm grant com.example.sample.myapplication android.permission.BATTERY_STATS\");
Does it require root permission to give this permission?
If it is so, why it does not require the device to be rooted to run it via adb shell?
I am new to Android, please explain a bit more clearly what is happening inside and how to proceed.
回答1:
The command you run with adb shell gets executed with shell(UID=2000) user privileges. The command you run from your java code gets executed with your app's UID privileges. Thus the difference.
来源:https://stackoverflow.com/questions/38824114/understanding-command-through-adb-shell-and-through-code-android