2014-09-10 81 views
-1

我試圖以編程方式重啓我的Galaxy S3。我已經試過試圖以編程方式重啓根設備Android設備

事情:

try { 
     Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "reboot" }); 
     proc.waitFor(); 
    } catch (Exception ex) { 
     Log.i("RebootActivity", "Could not reboot", ex); 
    } 

    try { 
     Runtime.getRuntime().exec(new String[]{"su","-c","reboot now"}); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    try 
    {   
     Process process = Runtime.getRuntime().exec("su"); 
     DataOutputStream os = new DataOutputStream(process.getOutputStream()); 
     os.writeBytes("reboot now\n"); 
    } 
    catch (Throwable t) 
    { 
     t.printStackTrace(); 
    } 

你們是否有任何想法如何做到這一點?

謝謝。

+0

如果我嘗試使用'/ system/bin/su',則會得到以下異常:java.io.IOException:運行exec()時發生錯誤。命令:[/ system/bin/su,-c,now now]工作目錄:null環境:null' – 2014-09-10 19:13:55

+0

每次重新啓動模擬器時,必須輸入'「adb shell chmod 777/dev/tty <>」' (當然用您的串口設備替換/ dev/tty <>「) – 2014-09-10 19:26:49

+0

@SagarPilkhwal,我沒有使用模擬器我正在使用Galaxy S3 4.4.2 – 2014-09-10 19:38:18

回答

0

試着做一個正常的字符串看起來像「蘇\ n重新啓動; \ n「而不是數組。 嘗試從shell獲得答案,這有助於調試。

你的su二進制文件的權限是什麼?如果他們錯了,你可以在「mount -o remount,rw/system」之後嘗試「chmod 7777/system/xbin/su」

下面是一些示例代碼:(要運行字符串命令,一\ n和;或Linux的shell命令的分隔列表)

StringBuffer commandOutput = new StringBuffer(); 
    try { 
     Process process = Runtime.getRuntime().exec("su\n"); 
     DataOutputStream out = new DataOutputStream(process.getOutputStream()); 
     out.writeBytes("export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/vendor/lib:/system/lib\n"); 
     out.writeBytes(command+"\n"); 
     out.flush(); 

     process.waitFor(); 
     BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); 
     int numRead; 
     char[] buffer = new char[1000]; 
     while ((numRead = in.read(buffer)) > 0) { 
      commandOutput.append(buffer, 0, numRead); 
     } 
     in.close(); 
     process.waitFor(); 
    } catch ... 

    return commandOutput.toString(); 
+0

如何從shell獲得答案? ! – 2014-09-10 19:35:17

+0

此代碼返回此錯誤:'W/System.err(26960):java.io.IOException:寫入失敗:EPIPE(Broken pipe)' – 2014-09-10 19:58:56

+0

也許這有助於http://stackoverflow.com/questions/17615412/android -write-failed-epipe-broken-pipe-error-on-write-file – user3387542 2014-09-10 20:02:08

相關問題