简体   繁体   中英

libsuperuser get command output in real time

I've never used libsuperuser but only 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. 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. But with libsuperuser I have only found Shell.SU.run() that returns the output but only when the process is finished. How can I read the output lines in realtime with libsuperuser?

You must use Shell.Interactive.addCommand() with its callback:

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.

See sample code "InteractiveActivity" from Chainfire's libsuperuser repository.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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