android系統重啟裝置

來源:互聯網
上載者:User

標籤:

第一種方法:

記得有一本書上介紹 說 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系統重啟裝置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.