2010-09-27 7 views

Répondre

8

Cet extrait nécessite un accès root, mais exécutera la chaîne donnée comme une commande shell

void execCommandLine(String command) 
{ 
    Runtime runtime = Runtime.getRuntime(); 
    Process proc = null; 
    OutputStreamWriter osw = null; 

    try 
    { 
     proc = runtime.exec("su"); 
     osw = new OutputStreamWriter(proc.getOutputStream()); 
     osw.write(command); 
     osw.flush(); 
     osw.close(); 
    } 
    catch (IOException ex) 
    { 
     Log.e("execCommandLine()", "Command resulted in an IO Exception: " + command); 
     return; 
    } 
    finally 
    { 
     if (osw != null) 
     { 
      try 
      { 
       osw.close(); 
      } 
      catch (IOException e){} 
     } 
    } 

    try 
    { 
     proc.waitFor(); 
    } 
    catch (InterruptedException e){} 

    if (proc.exitValue() != 0) 
    { 
     Log.e("execCommandLine()", "Command returned error: " + command + "\n Exit code: " + proc.exitValue()); 
    } 
} 
+0

Merci beaucoup ... ce serait nécessaire de modifier cette option pour exécuter plusieurs commandes shell? –

+0

Dans le pire des cas, vous pouvez appeler cette fonction pour chaque commande, mais vous devriez pouvoir séparer chaque commande avec un caractère de nouvelle ligne \ n –

Questions connexes