2013-05-18 112 views
0

我需要在我的應用程序中執行一個C程序,只需將可執行文件添加到android項目並構建.apk。然後,我嘗試在我的應用程序來執行這樣的程序:android模擬器root訪問

Process result = Runtime.getRuntime().exec("su"); 
    String cmd = "PROGRAM_NAME"; 

    DataOutputStream dos = new DataOutputStream(result.getOutputStream()); 
    DataInputStreamdis = new DataInputStream(result.getInputStream()); 

    dos.writeBytes(cmd + "\n"); 
    dos.writeBytes("exit\n"); 
    dos.flush(); 

我知道我需要root權限才能做到這一點,所以我安裝Superuser.apk,但沒有奏效。是否有另一種可能的方式來做到這一點?順便說一句代碼沒有完全展開它應該只是給看一下程序應該被執行 我運行仿真器與Android 4.2.1

編輯方式,即用

  Process suProcess = Runtime.getRuntime().exec("su"); 

     DataOutputStream os = new DataOutputStream(suProcess.getOutputStream()); 
     DataInputStream osRes = new DataInputStream(suProcess.getInputStream()); 

     if (null != os && null != osRes) { 
      os.writeBytes("id\n"); 
      os.flush(); 
      String currUid = osRes.readLine(); 
      boolean exitSu = false; 
      if (null == currUid) { 
       Log.d("ROOT", "Can't get root access or denied by user"); 
      } 
檢查root權限第一
+0

請參閱這裏的答案:http://stackoverflow.com/questions/5095234/how-to-get-root-access-on-android-emulator?rq=1 –

+0

使用Android 2.2版本這就是爲什麼它的工作他們,我使用4.2.1的Root.apk /超級用戶.apk,認爲它是一樣的,不工作!? – wasp256

+0

Superuser.apk不存在設備。嘗試第二個答案的說明,並嘗試以讀/寫方式重新掛載/系統文件系統,然後按su命令。 –

回答

0

我有和你一樣的問題。你可以找到很多像here這樣的答案,但是他們太老了,他們不再工作了(在新的sdk中)。

我找到了最好的答案here其說,Security Tips of Android不會允許任何開發人員有root訪問權:

A central design point of the Android security architecture is that no application, by default, 
has permission to perform any operations that would adversely impact other applications, the 
operating system, or the user. This includes reading or writing the user's private data (such as 
contacts or e-mails), reading or writing another application's files, performing network access, 
keeping the device awake, etc. 

,讓你有應用層之下的唯一途徑是通過權限。