通过ADB Shell和代码了解命令-Android [英] Understanding command through adb shell and through code - Android

查看:156
本文介绍了通过ADB Shell和代码了解命令-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为应用授予BATTERSTATS权限.当我从PC上运行命令时-可以正常工作:

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

但是从Android应用程序运行时,相同的pm grant命令不起作用:

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

是否需要root权限才能授予此权限? 如果是这样,为什么不要求将设备植根以通过adb shell运行它?

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?

我是Android的新手,请更清楚地解释一下内部正在发生什么以及如何进行.

I am new to Android, please explain a bit more clearly what is happening inside and how to proceed.

推荐答案

使用adb shell运行的命令将以shell(UID=2000)用户权限执行.从java代码运行的命令将使用应用程序的UID特权执行.因此有所不同.

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.

这篇关于通过ADB Shell和代码了解命令-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆