简体   繁体   中英

Android Nougat, Runtime.exec won't work

I'm trying to create a terminal emulator. My code is

ProcessBuilder builder = new ProcessBuilder("/system/bin/sh");
builder.redirectErrorStream(true);
builder.directory(f);
p = builder.start();

stdOut = p.getInputStream();
stdIn = p.getOutputStream();

stdIn.write(("ls\n").getBytes());
stdIn.write(("echo EOF\n").getBytes());

while(true) {
    int n;
    try {
        n = stdOut.read();
    } catch (Exception | Error e) {
        return;
    }

    if(n == -1) continue;

    char c = (char) n;
    if(c == '\n') {
        if(line.equals("EOF")) break;
        else {
            if(write) outputable.onOutput(line);
            outputMsg[0] = outputMsg[0] + "\n" + line;
            line = "";
        }
    } else {
        line = line + c;
    }
}

This code works on Android 6.0, 5.0, 4.0, 3.0... It doesn't work on Nougat (7.0). No errors, no exceptions, nothing. It just hangs on this line:

n = stdOut.read();

InputStream .read() should hang only when there's nothing available in the stream. Does someone know the solution?

I solved my problem using Chainfire's library libsuperuser . Not a solution, but at least a workaround. And, one could also investigate the code, looking for a decisive difference, if he wants.

That's not my case, libsuperuser works like a charm.

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