Android System restart device

Source: Internet
Author: User

The first method:

Remember that there is a book that says 0 permission to restart the phone, the principle is that when a toast is displayed on the Android system, the toast is actually attached to the form, and the form is a service of the system, and if the toast is continuously mounted to the form in the unit time, will continue to apply for system memory, causing the system to restart.

private void Anr () {while (true) {System.out.println ("running."); Toast toast = new Toast (Getapplicationcontext ()); View Toastview = new View (Getapplicationcontext ()); Toast.setview (Toastview); Toast.show ();}}

After testing with the Xiaomi 3 real machine, it was found that only the program was ANR and the device could not be restarted.

The second method:

Using Su, the Change method requires the application to get root permissions

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.prin Tstacktrace ();}}}                    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) {}}
It is feasible to test by real machine.

Third, the principle of the same, requires root permission, runtime is used to execute the Linux shell command, through which can implement the device related operations

Related articles

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 ();}}

Fourth, using Powermanger to restart the device, the same need to add permissions in the configuration file 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. Won't 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.


Sample Demo



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android System restart device

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.