gpt4 book ai didi

java - libsuperuser 实时获取命令输出

转载 作者:行者123 更新时间:2023-11-30 02:27:08 25 4
gpt4 key购买 nike

我从未使用过 libsuperuser,只使用过 roottools。现在我想切换到 libsuperuser,但我找不到以 root 身份调用命令并读取其输出而无需等待命令完成的方法。使用 roottools 很容易,因为它有一个方法,每次进程将新行写入 stdout 时都会调用该方法。但是对于 libsuperuser,我只发现 Shell.SU.run() 返回输出,但仅在进程完成时返回。如何使用 libsuperuser 实时读取输出行?

最佳答案

您必须使用 Shell.Interactive.addCommand() 及其回调:

Shell.Interactive rootSession = new Shell.Builder().useSU().open(/*...*/);

session 打开后,您可以添加命令:

rootSession.addCommand(new String[] { "ls -l /sdcard" },
1, // a command id
new Shell.OnCommandLineListener() {
@Override
public void onCommandResult(int commandCode, int exitCode) {
// ...
}
@Override
public void onLine(String line) {
// ...
}
});

onLine(String line) 就是您要查找的内容。

参见 sample code "InteractiveActivity"来自 Chainfire 的 libsuperuser 存储库。

关于java - libsuperuser 实时获取命令输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27802795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com