繁体   English   中英

libsuperuser实时获取命令输出

[英]libsuperuser get command output in real time

我从未使用过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”

暂无
暂无

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

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