2015-04-06 70 views
2

我正嘗試在android文件系統中從/ dev/test節點讀寫數據。 我試圖從android應用程序的根設備中的根文件系統中的/ dev/test文件讀寫數據

mount -o rw remount /dev && touch /dev/test 

從外殼和它的工作,但是當我嘗試使用

Runtime rt = Runtime.getRuntime(); 
Process proc = rt.exec("su"); 
proc = rt.exec("mount -o rw remount /dev && touch /dev/test"); 

沒有名爲/ dev目錄下的測試文件/

+0

/dev不是普通文件的位置。用'touch'創建一個是一個錯誤。使用'mknod',但不要期望結果像普通文件一樣運行。你究竟在做什麼**來完成**? – 2015-04-06 12:49:53

+0

我正在創建一個將寫入文件並由設備驅動程序輪詢的系統服務。 – Odin 2015-04-19 19:20:33

+1

除了對任務使用錯誤的操作(如前所述,您必須使用帶有適當的主號碼和次號碼以及設備類型而不是「touch」的'mknod'),您不會將其作爲超級用戶運行。每個對exec()的調用都是獨立的 - 你調用su只會退出,然後嘗試將其他命令作爲應用程序的用戶運行,而不是以root身份運行。 – 2015-04-19 20:25:22

回答

0

嘗試用這種方法給exec它:

private boolean performScript(String script) { 
     script = "mount -o rw remount /dev && touch /dev/test"; 
     try { 
      // Executes the command. 
      Process process = Runtime.getRuntime().exec("su"); 

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

//    // Reads stdout. 
//    // NOTE: You can write to stdin of the command using process.getOutputStream(). 
//    final BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); 
//    int read; 
//    final char[] buffer = new char[4096]; 
//    while ((read = reader.read(buffer)) > 0) { 
//     stringBuilder.append(buffer, 0, read); 
//     txtInfo.setText(stringBuilder.toString()); 
//    } 
//    reader.close(); 

      // Waits for the command to finish. 
      process.waitFor(); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
     catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 

這裏的訣竅是運行腳本作爲超級用戶。

+0

試過了。 – Odin 2015-04-06 10:05:21

+0

它是否要求您獲得SU權限?是否允許它? – Stan 2015-04-06 10:08:37

+0

也嘗試讀取'stdout'以知道發生了什麼。 – Stan 2015-04-06 10:11:50

相關問題