android Runtime.getRuntime().exec使用
阿新 • • 發佈:2019-02-02
1.read value:
public String readRegister(String path){ try { Process process = Runtime.getRuntime().exec("/system/bin/cat "+path); BufferedReader reader = new BufferedReader( new InputStreamReader(process.getInputStream())); int read; char[] buffer = new char[4096]; StringBuffer output = new StringBuffer(); while ((read = reader.read(buffer)) > 0) { output.append(buffer, 0, read); } reader.close(); // Waits for the command to finish. process.waitFor(); System.out.println(output.toString()); valRegister = output.toString(); return valRegister; } catch (IOException e) { throw new RuntimeException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } }
2.change mode:
Runtime.getRuntime().exec("su -c chmod 777 /data/glad.txt");
3.write value:
Runtime.getRuntime().exec("/system/bin/sh /data/test.sh");
test.sh:
echo 777 > /data/glad.txt
ps: You cannot use Runtime.exec() like a command line你不能用Runtime.exec()來執行像命令列那樣的程式,如重定向輸出:Process proc = rt.exec("java jecho 'Hello World' > test.txt");
這段程式的意圖是執行一個名為jecho的Java程式,並將其輸出重定向到test.txt檔案中。 但這種寫法得不到正確結果,因為Runtime.exec()方法並不能很好的執行這種較為複雜的命令列,必須通過自己處理程式輸出並寫入到相應的檔案
ps:
寫操作需要設定apk為系統應用。