简体   繁体   English

libsuperuser实时获取命令输出

[英]libsuperuser get command output in real time

I've never used libsuperuser but only roottools. 我从未使用过libsuperuser,而仅使用过roottools。 Now I want to switch to libsuperuser but I don't find a way to call a command as root and read its output without wait for comand finish. 现在,我想切换到libsuperuser,但是我没有找到一种方法来以root身份调用命令并读取命令的输出,而无需等待命令完成。 With roottools it was easy because it has a method that's called each time a new line is written to stdout by the process. 使用roottools很容易,因为它有一个方法,该方法每次在进程将新行写入stdout时都被调用。 But with libsuperuser I have only found Shell.SU.run() that returns the output but only when the process is finished. 但是对于libsuperuser,我仅找到Shell.SU.run()返回该输出,但仅在该过程完成时才返回输出。 How can I read the output lines in realtime with libsuperuser? 如何使用libsuperuser实时读取输出行?

You must use Shell.Interactive.addCommand() with its callback: 您必须使用Shell.Interactive.addCommand()及其回调:

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

Once the session is open, you can add commands: 会话打开后,您可以添加命令:

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) {
            // ...
        }
    });

The onLine(String line) is what you are looking for. 您正在寻找onLine(String line)

See sample code "InteractiveActivity" from Chainfire's libsuperuser repository. 请参阅Chainfire的libsuperuser存储库中的示例代码“ InteractiveActivity”

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

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