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());
}
}
Merci beaucoup ... ce serait nécessaire de modifier cette option pour exécuter plusieurs commandes shell? –
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 –