2010-09-27 44 views

Trả lời

8

đ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()); 
    } 
} 
+0

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? –

+0

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 –

Các vấn đề liên quan