这是我正在尝试制作的示例:
final String[] hwdebug1 = {"su","-c","echo hello > /system/hello1.txt"};
try {
Runtime.getRuntime().exec(hwdebug1);
} catch (IOException e) {
}
所以,如果我点击我的按钮,它会完美地工作,但如果我做了类似的事情,它就不会工作:
final String[] hwdebug1 = {"su","-c","echo hello > /system/hello1.txt","echo hello > /system/hello2.txt","echo hello > /system/hello3.txt"};
我的目的是让按钮执行多个命令。我已经通过让它执行 bash 脚本来做到这一点,但我更喜欢找到一种将其放在代码中的方法。
谢谢!
用Ben75方法解决
final String[] hwdebug1 = {"su","-c","echo hello > /system/hello1.txt"};
final String[] hwdebug2 = {"su","-c","echo hello > /system/hello2.txt"};
final String[] hwdebug3 = {"su","-c","echo hello > /system/hello3.txt"};
ArrayList<String[]> cmds = new ArrayList<String[]>();
cmds.add(hwdebug1);
cmds.add(hwdebug2);
cmds.add(hwdebug3);
for(String[] cmd:cmds){
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
e.printStacktrace();
}
}
最佳答案
Runtime.exec 命令适用于单个命令,不是 cmd 行字符串的简单“包装器”。
只需创建一个列表并对其进行迭代即可:
final String[] hwdebug1 = {"su","-c","echo hello > /system/hello1.txt"};
final String[] hwdebug2 = {"su","-c","echo hello > /system/hello2.txt"};
final String[] hwdebug3 = {"su","-c","echo hello > /system/hello3.txt"};
ArrayList<String[]> cmds = new ArrayList<String[]>();
cmds.add(hwdebug1);
cmds.add(hwdebug2);
cmds.add(hwdebug3);
for(String[] cmd:cmds){
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
e.printStacktrace();
}
}
关于java - 使用 onClick 执行多个 shell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14498177/