Java以超級使用者身份執行系統命令的方式
阿新 • • 發佈:2019-02-01
執行Linux系統命令, 如果無法獲取root許可權, 會丟擲異常
public void testDate(){
Log.i("zmr", "testDate");
try {
Process process = Runtime.getRuntime().exec("su");
String datetime="20131023.112800"; //測試的設定的時間【時間格式 yyyyMMdd.HHmmss】
DataOutputStream os = new DataOutputStream(process.getOutputStream ());
os.writeBytes("setprop persist.sys.timezone GMT\n");
os.writeBytes("/system/bin/date -s "+datetime+"\n");
os.writeBytes("clock -w\n");
os.writeBytes("exit\n");
os.flush();
} catch (IOException e) {
e.printStackTrace();
Log.e("zmr", "testDate e= " + e.getLocalizedMessage());
}
}