0

here is an example of what i'm trying to make:

final String[] hwdebug1 = {"su","-c","echo hello > /system/hello1.txt"};

                 try {
                     Runtime.getRuntime().exec(hwdebug1);              
                } catch (IOException e) {
                }            

so, if i click my button, it works perfectly, But it doesn't if, example, i do something like that:

final String[] hwdebug1 = {"su","-c","echo hello > /system/hello1.txt","echo hello > /system/hello2.txt","echo hello > /system/hello3.txt"};

My intent is to let the button exec more than 1 command. I already did it by let it execut a bash script but i prefer to find a way to put it on code.

Thanks!

Solved with Ben75 Method

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(); 
   }          
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
iGio90
  • 290
  • 1
  • 4
  • 15

1 Answers1

2

The Runtime.exec command is working for one single command and is not a simple "wrapper" for a cmd line string.

Just create a List and iterate on it:

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(); 
   }          
}
ben75
  • 29,217
  • 10
  • 88
  • 134
  • Hello Ben, your method works and i'm actually using it. I got one question related. executing su -c cmd will display a popup showing the command executed on the device. Is there any way to avoid it? i tried to send it to >> /dev/null but didn't work! – iGio90 Jan 25 '13 at 00:43