嘗試用這種方法給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();
}
}
這裏的訣竅是運行腳本作爲超級用戶。
/dev不是普通文件的位置。用'touch'創建一個是一個錯誤。使用'mknod',但不要期望結果像普通文件一樣運行。你究竟在做什麼**來完成**? – 2015-04-06 12:49:53
我正在創建一個將寫入文件並由設備驅動程序輪詢的系統服務。 – Odin 2015-04-19 19:20:33
除了對任務使用錯誤的操作(如前所述,您必須使用帶有適當的主號碼和次號碼以及設備類型而不是「touch」的'mknod'),您不會將其作爲超級用戶運行。每個對exec()的調用都是獨立的 - 你調用su只會退出,然後嘗試將其他命令作爲應用程序的用戶運行,而不是以root身份運行。 – 2015-04-19 20:25:22