2011-05-16 5 views
0

내 Java 응용 프로그램에서 쉘 명령을 실행하기위한 다음과 같은 메소드가 있는데 사용자 폰의 모든 응용 프로그램에 대한 권한을 수정하는 것과 같은 스크립트를 실행하려고합니다. 이 명령을 사용하여 스크립트를 아무 문제없이 실행할 수 있습니다. execCommand ("/ system/xbin/fix_perm"); 그러나 문제는 터미널 에뮬레이터처럼 출력되는 것을 출력하고 싶습니다. 어떻게하면 출력 스트림을 받아 화면에 출력 할 수 있습니까? 난 당신이 su으로 임의의 명령을 실행하는 사용자를 허용하는 잠재적으로 극단적 인 결과를 인식하고 대신 가능한 솔루션을 가리 킵니다 바라고 도움쉘 스크립트의 프로세스 표시

public Boolean execCommand(String command) 
{ 
    try { 
     Runtime rt = Runtime.getRuntime(); 
     Process process = rt.exec("su"); 
     DataOutputStream os = new DataOutputStream(process.getOutputStream()); 
     os.writeBytes(command + "\n"); 
     os.flush(); 
     os.writeBytes("exit\n"); 
     os.flush(); 
     process.waitFor(); 
    } catch (IOException e) { 
     return false; 
    } catch (InterruptedException e) { 
     return false; 
    } 
    return true; 
} 

답변

0

주셔서 감사합니다.

public Boolean execCommand(String command) 
{ 
    try { 
     Runtime rt = Runtime.getRuntime(); 
     Process process = rt.exec("su"); 

     // capture stdout 
     BufferedReader stdout = new BufferedReader(
      new InputStreamReader(process.getInputStream())); 
     // capture stderr 
     BufferedReader stderr = new BufferedReader(
      new InputStreamReader(process.getErrorStream())); 

     DataOutputStream os = new DataOutputStream(process.getOutputStream()); 
     os.writeBytes(command + "\n"); 
     os.flush(); 

     String line = null; 
     StringBuilder cmdOut = new StringBuilder(); 
     while ((line = stdout.readLine()) != null) { 
      cmdOut.append(line); 
     } 
     stdout.close(); 
     while ((line = stderr.readLine()) != null) { 
      cmdOut.append("[ERROR] ").append(line); 
     } 
     stderr.close(); 

     // Show simple dialog 
     Toast.makeText(getApplicationContext(), cmdOut.toString(), Toast.LENGTH_LONG).show(); 

     os.writeBytes("exit\n"); 
     os.flush(); 

     // consider dropping this, see http://kylecartmell.com/?p=9 
     process.waitFor(); 
    } catch (IOException e) { 
     return false; 
    } catch (InterruptedException e) { 
     return false; 
    } 
    return true; 
} 
+0

안드로이드 기기에서 유용한 기능을 하나라도 수행합니까? –

+0

(IDE에 표시 할 수 있음) Android 로거를 사용하도록 변경했습니다. –

+0

예 ...하지만 OP가 장치의 화면에 표시하려고한다고 생각합니다. –

관련 문제