读取 su 进程内的命令输出
首先我介绍一下我的情况。 我需要在我的 Android 应用程序中执行“su”命令,它运行良好。然后我需要执行“ls”命令并读取输出。我通过从“su”进程获取输出流并将我的命令写入其中来完成此操作。
问题来了。如何读取“ls”进程的输出?我所拥有的只是“su”进程对象。从中获取输入流不会给出任何结果,因为“su”不写任何内容。但“ls”确实如此,但我不知道如何访问其输出消息。
我搜索了很多网站,但没有找到任何解决方案。也许有人会帮助我:)
问候
firstly I will present my situation.
I need to execute "su" command in my android app and it works well. Then I need to execute "ls" command and read the output. I'm doing it by getting the output stream from the "su" process and writing my command into it.
And here goes the question. How to read the output of the "ls" process? All I have is the "su" Process object. Getting the input stream from it gives nothing, because "su" doesn't write anything. But "ls" does and I don't know how to access its output messages.
I have searched many sites but I didn't find any solution. Maybe someone will help me:)
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,我找到了解决方案。它应该看起来像这样:
希望对某人有帮助
Ok, I've found a solution. It should look like this:
Hope it will be helpful for someone
检查此处提到的代码:
如何运行终端命令Android 应用程序?
参考文献
这段代码
GScript
Check this code mentioned here:
How to run terminal command in Android application?
References
this code
GScript
我针对以下问题修改了 @glodos 接受的答案:
ps
(即adb shell
)多次执行后,您将看到几个
su
进程活。他们需要被正确终止。
waitFor()
以确保进程终止。read=-1
的处理,现在可以执行带有空stdout
的命令。以前,它们在new String(buffer, 0, read)
上崩溃。使用
StringBuffer
进行更高效的字符串处理。一些积分归@Sherif elKhatib ))
I modified accepted answer by @glodos for following problems:
ps
in shell (ieadb shell
)after several executions then you'll see several
su
processesalive. They needs to be properly terminated.
waitFor()
to make sure the process is terminated.read=-1
, now commands with emptystdout
can be executed. Previously they crashed onnew String(buffer, 0, read)
Using
StringBuffer
for more efficient strings handling.Some credits go to @Sherif elKhatib ))