2012-07-06 77 views
3

su命令我試圖執行以下方法:執行從機器人活動

String[] cmds = {"/system/bin/sendevent /dev/input/event0 1 107 0 \n", "sleep 1", "/system/bin/sendevent /dev/input/event0 1 107 1 \n"}; 
       try { 
        runAsRoot(cmds); 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

但在logcat的i。接收如下錯誤::

public void runAsRoot(String[] cmds) throws Exception { 
     Process p = Runtime.getRuntime().exec("su"); 
     DataOutputStream os = new DataOutputStream(p.getOutputStream()); 
     InputStream is = p.getInputStream(); 
     for (String tmpCmd : cmds) { 
      os.writeBytes(tmpCmd+"\n"); 
      int readed = 0; 
      byte[] buff = new byte[4096]; 

      // if cmd requires an output 
      // due to the blocking behaviour of read(...) 
      boolean cmdRequiresAnOutput = true; 
      if (cmdRequiresAnOutput) { 
       while(is.available() <= 0) { 
        try { Thread.sleep(200); } catch(Exception ex) {} 
       } 

       while(is.available() > 0) { 
        readed = is.read(buff); 
        if (readed <= 0) break; 
        String seg = new String(buff,0,readed); 
        Log.i("#>", seg); 
       } 
      } 
     }   
     os.writeBytes("exit\n"); 
     os.flush(); 
    } 

我使用以下輸入調用此方法

07-06 15:19:27.007: E/su(6547): sudb - Opening database 
07-06 15:19:27.007: E/(6547): Couldn't open database: unable to open database file 
07-06 15:19:27.017: E/su(6547): sudb - Could not open database, prompt user 
07-06 15:19:47.082: E/su(6547): select failed with 2: No such file or directory 
07-06 15:19:47.082: W/su(6547): request rejected (10060->0 /system/bin/sh) 

任何想法是什麼問題?

+0

是你的手機植根? – Blackbelt 2012-07-06 09:59:24

+0

我正在模擬器上工作,它的根源是 – 2012-07-06 10:03:53

回答

4

看起來像su二進制文件存在問題,而不是您的應用程序。檢查您是否可以成功運行'adb shell'中的rooted shell。 如果'adb shell'從開始就爲您提供了rooted shell,請運行'su 1000'以失去root權限,然後運行'su'嘗試再次進入rooted shell。如果失敗了,su不起作用。

哦,並在相關說明:一定要在另一個線程,可能通過處理程序或AsyncTask運行su,所以它不會阻止你的UI線程。

+0

是的,我的su不工作。我應該再次安裝它。 – 2012-07-06 10:18:08

+0

我猜你安裝錯了。您可以執行以下操作以在adb外殼中創建一個非常不安全但工作的su二進制文件: cat/system/bin/sh>/system/bin/su - 點擊輸入 - chmod 6755/system/bin/su – 2012-07-06 14:38:25

+0

我在froyo設備上測試過它,它對我來說工作得很好。另外,我用chmode 06755,命令執行成功,但最終結果與我發佈的相同。 – 2012-07-09 05:26:47