đoạn này đòi hỏi quyền root, nhưng sẽ thực hiện chuỗi được coi là một lệnh 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());
}
}
Nguồn
2010-09-27 21:10:21
cảm ơn bạn rất nhiều ... những gì sẽ được yêu cầu để sửa đổi này để chạy nhiều lệnh shell? –
Trường hợp xấu nhất bạn có thể gọi hàm này cho mỗi lệnh, nhưng bạn sẽ có thể tách riêng mỗi lệnh bằng ký tự dòng mới \ n –