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

查看:33
本文介绍了通过 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权限?如果是这样,为什么不需要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天全站免登陆