我从未使用过libsuperuser,而仅使用过roottools。现在,我想切换到libsuperuser,但是我没有找到一种方法来以root身份调用命令并读取命令的输出,而无需等待命令完成。使用roottools很容易,因为它有一个方法,该方法每次在进程将新行写入stdout时都被调用。但是使用libsuperuser我只发现Shell.SU.run()
仅在过程完成时才返回输出。如何使用libsuperuser实时读取输出行?
您必须使用Shell.Interactive.addCommand()
其回调:
Shell.Interactive rootSession = new Shell.Builder().useSU().open(/*...*/);
会话打开后,您可以添加命令:
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)
就是您想要的。
请参阅Chainfire的libsuperuser存储库中的示例代码“ InteractiveActivity”。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句