简体   繁体   English

Root 访问适用于 adb,但 Android Studio 上的 root 进程不适用 (Android Things)

[英]Root access works with adb, but root process on Android Studio doesn't (Android Things)

Currently I'am working on an aplication via Android Studio, that executes commands using the next sintaxis:目前我正在通过 Android Studio 开发一个应用程序,它使用下一个 sintaxis 执行命令:

Process p = Runtime.getRuntime().exec(new String[] { "su", "-c", "ip link show"});

I'am working with the device IMX7D_PICO (it uses Android Things as SO).我正在使用设备 IMX7D_PICO(它使用 Android Things 作为 SO)。 It is rooted, as is shown in the following pic:它是root的,如下图所示:

在此处输入图片说明

But, when I run a command as root on Android Studio, I get the next error:但是,当我在 Android Studio 上以 root 身份运行命令时,出现下一个错误:

W/System.err: java.io.IOException: Cannot run program "su": error=13, Permission denied
W/System.err:     at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:692)
at java.lang.Runtime.exec(Runtime.java:560)

I don't know why su works only on adb.我不知道为什么su只适用于 adb。 In other hand, I know that there are others ways to obtain the data of ip link show , but the next step, it's open a socket RAW with the command that I said.另一方面,我知道还有其他方法可以获取ip link show的数据,但是下一步,它是使用我说的命令打开套接字 RAW。 So, I need to run process as su via Android Studio.所以,我需要通过 Android Studio 以su身份运行进程。

An app's process has less privileges compared to those of the shell 's, resulting inshell的进程相比,应用程序的进程具有较少的权限,导致

java.io.IOException: Cannot run program "su": error=13, Permission denied java.io.IOException:无法运行程序“su”:错误=13,权限被拒绝

For the permission to be granted you should install SuperSU app or alike and follow the app's prompts while your app is trying to su with Runtime.getRuntime().exec("su") .对于要授予权限,你应该安装SuperSU应用程序或相似,并按照程序的提示,而您的应用程序试图suRuntime.getRuntime().exec("su")

Once the process has gotten root you can grab the standard input of the root process and write the command(s) to it, reading its standard output.一旦进程获得root您就可以获取root进程的标准输入并将命令写入其中,读取其标准输出。 For more details see: execute shell command from android .有关更多详细信息,请参阅:从 android 执行 shell 命令

I don't know why su doesn't work.我不知道为什么 su 不起作用。 I 'suspect' that Android Things is using a single user that is root already, so maybe executing the command without using su would work.我“怀疑”Android Things 正在使用一个已经是 root 的单个用户,所以也许在不使用 su 的情况下执行命令会起作用。

On the other part of your question.在你问题的另一部分。 Parsing the output of system commands is not the best way of getting info, in this case, you can probably get what you want using the Android class NetworkInterface https://developer.android.com/reference/java/net/NetworkInterface#getNetworkInterfaces() .解析系统命令的输出并不是获取信息的最佳方式,在这种情况下,您可能可以使用 Android 类NetworkInterface https://developer.android.com/reference/java/net/NetworkInterface#getNetworkInterfaces获取您想要的内容() .

You can manage raw sockets using the Android framework classes too.您也可以使用 Android 框架类管理原始套接字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM