0

这是我想要做的一个例子:

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"};

我的意图是让按钮执行超过 1 个命令。我已经通过让它执行一个 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(); 
   }          
}
4

1 回答 1

2

Runtime.exec 命令适用于单个命令,而不是cmd 行字符串的简单“包装器”。

只需创建一个 List 并对其进行迭代:

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(); 
   }          
}
于 2013-01-24T09:54:38.797 回答