如果我理解正确,那么您要做的就是将单行
示例方法更改为可以接受并发送多行的方法,如下所示:
public Boolean execCommands(String... command) { try { Runtime rt = Runtime.getRuntime(); Process process = rt.exec("su"); DataOutputStream os = new DataOutputStream(process.getOutputStream()); for(int i = 0; i < command.length; i++) { os.writeBytes(command[i] + "n"); os.flush(); } os.writeBytes("exitn"); os.flush(); process.waitFor(); } catch (IOException e) { return false; } catch (InterruptedException e) { return false; } return true; }这样,您可以像这样调用多行bash命令:
String[] commands = { "echo 'test' >> /sdcard/test1.txt", "echo 'test2' >>/sdcard/test1.txt" }; execCommands(commands); String commandText = "echo 'foo' >> /sdcard/foo.txtnecho 'bar' >> /sdcard/foo.txt"; execCommands(commandText.split("n"));


