-1

I`m currently using the this code to send su commands:

   private void execShellCmd(String cmd) {
    try {
        Process process = Runtime.getRuntime().exec("su");
        OutputStream outputStream = process.getOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
        dataOutputStream.writeBytes(cmd);
        dataOutputStream.flush();
        dataOutputStream.close();
        outputStream.close();
    } catch (Throwable e) {

    }
}

During App Runtime i need to execute several commands, but with my code the su toast popup every time and is blocking view for 3 seconds.

How can I use the su session for multiple commands?

e.g.

private void initexecShell()
private void execShellCmd(String cmd)
private void execShellCmd(String cmd)
private void execShellCmd(String cmd)
private void closeexecShell()

Thanks a lot for you help!

Edit: for example I tried the following, but does not work:

private void test() {
initexecShell();
execShellCmd("command1");
//do something else
execShellCmd("command2");
//do something
execShellCmd("command3");
closeexecShell();

}

Process process;
private void initexecShell()
{
    Process process = Runtime.getRuntime().exec("su");
}
     
private void execShellCmd(String cmd) {
    try {
        
        OutputStream outputStream = process.getOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
        dataOutputStream.writeBytes(cmd);
        dataOutputStream.flush();
        dataOutputStream.close();
        outputStream.close();
    } catch (Throwable e) {

    }
}
private void closeexecShell()
{
   //?? no idea 
}
luki1001
  • 1
  • 1
  • Your suggestion seems ok. So, what's wrong with it? – Seelenvirtuose Feb 25 '22 at 11:24
  • How can I modify my function to start only one single su shell and use it again when i need it. To avoid su toast popup again for every command. – luki1001 Feb 25 '22 at 13:00
  • Have you granted the permission for your app? Click the "always granted" checkbox or something when the popup appears. That way, it will never show up again. I'd recommend to use `ProcessBuilder` instead, because it can redirect error stream to input stream, so that you don't to check both input streams. – Darkman Feb 25 '22 at 13:37
  • yes i have granted only once and it works for all commands i send. But I get a 3 seconds su info message for every command. I like to reuse the open su session and not starting a new one for each command – luki1001 Feb 25 '22 at 18:59
  • @luki1001 You should've called `sh` first instead of `su` e.g. `.exec("/system/bin/sh");`. And you can use `writeBytes()` multiple times to send the commands to the shell to be executed e.g. `writeBytes("su\n");`, `writeBytes("echo \"hello world\" > /sdcard/log.txt\n");`. After you've done with everything, don't forget to `writeBytes("exit\n");` and `flush()` then `close()`. – Darkman Feb 25 '22 at 19:43
  • Thanks a lot!, that works perfect for me, very nice. – luki1001 Feb 25 '22 at 20:00

1 Answers1

0
 private void test()
{
    try {
        SystemClock.sleep(5000); // ignore sleep, just for testing
        //It is important to apply for root permission, otherwise it will be useless
        Process process = Runtime.getRuntime().exec("/system/bin/sh");
        OutputStream outputStream = process.getOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
        dataOutputStream.writeBytes("su\n");
        dataOutputStream.writeBytes("echo \"hello world\" > /sdcard/log.txt\n");
        SystemClock.sleep(1000);
        dataOutputStream.writeBytes("echo \"hello world\" > /sdcard/log.txt\n");
        SystemClock.sleep(1000);
        dataOutputStream.writeBytes("echo \"hello world\" > /sdcard/log.txt\n");
        dataOutputStream.writeBytes("exit\n");
        dataOutputStream.flush();
        dataOutputStream.close();
        outputStream.close();
    } catch (Throwable e) {
        output1.setText(output1.getText() + "\n" + e.getMessage());
    }
}
luki1001
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 25 '22 at 21:41