標籤:
第一種方法:
記得有一本書上介紹 說 0許可權重啟手機,原理是 在android 系統中,當顯示一個toast,其實是將該toast掛載到表單上, 而表單又是系統的一個服務, 如果單位時間內不斷地向表單上掛載toast,就會不斷的申請系統記憶體,導致系統重新啟動。
private void anr() {while (true) {System.out.println("running.. ");Toast toast = new Toast(getApplicationContext());View toastView = new View(getApplicationContext());toast.setView(toastView);toast.show();}}
使用小米3真機測試過之後,發現只會導致程式ANR,並不能實現裝置重新啟動.
第二種方法:
使用SU,改方法需要應用擷取ROOT許可權
http://stackoverflow.com/questions/5484535/runtime-exec-reboot-in-android
public static void rebootSU() {Runtime runtime = Runtime.getRuntime();Process proc = null;OutputStreamWriter osw = null;StringBuilder sbstdOut = new StringBuilder();StringBuilder sbstdErr = new StringBuilder();String command="/system/bin/reboot";try { // Run Scriptproc = runtime.exec("su");osw = new OutputStreamWriter(proc.getOutputStream());osw.write(command);osw.flush();osw.close();} catch (IOException ex) {ex.printStackTrace();} finally {if (osw != null) {try {osw.close();} catch (IOException e) {e.printStackTrace(); }}}try {if (proc != null)proc.waitFor();} catch (InterruptedException e) {e.printStackTrace();}sbstdOut.append(new BufferedReader(new InputStreamReader(proc.getInputStream())));sbstdErr.append(new BufferedReader(new InputStreamReader(proc.getErrorStream())));if (proc.exitValue() != 0) {}}經真機測試,可行。
第三,原理同上,需要ROOT許可權, runtime是用來執行linux shell命令的,通過它可以實現對裝置的相關操作
相關文章
private void restart() { String cmd = "su -c reboot";//String cmd = "su -c shutdown";try {Runtime.getRuntime().exec(cmd);} catch (IOException e) {new AlertDialog.Builder(getApplicationContext()).setTitle("Error").setMessage(e.getMessage()).setPositiveButton("OK", null).show();}}
第四種, 使用powerManger 來重啟裝置,同樣的需要在設定檔中,添加許可權 android.permission.REBOOT
http://developer.android.com/reference/android/os/PowerManager.html#reboot(java.lang.String)
public void reboot (String reason)Added in API level 8Reboot the device. Will not return if the reboot is successful.Requires the REBOOT permission.Parametersreasoncode to pass to the kernel (e.g., "recovery") to request special boot modes, or null.
樣本DEMO
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
android系統重啟裝置