3

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.

3
  • Why are you executing that at runtime? Just add the permission to the manifest Commented Aug 8, 2016 at 8:09
  • Often commands you can use in adb shell require root if you execute the command at runtime in your application. If your want to grant permissions at runtime your should read this: developer.android.com/training/permissions/requesting.html Commented Aug 8, 2016 at 8:09
  • @cricket_007 I have given that in my manifest, but still its not working.. Commented Aug 8, 2016 at 8:14

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer....can you explain bref.....which means that shell(UID=2000) having access of super user permission or what...Explore little more with some doc
I'm not very savvy with shell. Can you please clarify how to solve the issue so that the java code gets executed with UID=2000? Or would that not be possible?(As you read on my posted question) I wish to do the same with a dpm command, would changing my apps UID privileges help? if so, how would I go about doing so? Thanks
do you know why running any wm command (e.g. wm size) works from adb but returns Can't find service: window from java (instead of some permission/user error). Different users do not explain why the command fails with can't find instead of an permission error...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.