Android 重启系统设备 或APP

源码下载:

https://download.csdn.net/download/qq_31939617/10481352下载

第一种方法:

记得有一本书上介绍 说 0权限重启手机,原理是 在android 系统中,当显示一个toast,其实是将该toast挂载到窗体上, 而窗体又是系统的一个服务, 如果单位时间内不断地向窗体上挂载toast,就会不断的申请系统内存,导致系统重新启动。

  1. private void anr() {  
  2.   
  3.         while (true) {  
  4.   
  5.             System.out.println(”running.. ”);  
  6.   
  7.             Toast toast = new Toast(getApplicationContext());  
  8.   
  9.             View toastView = new View(getApplicationContext());  
  10.   
  11.             toast.setView(toastView);  
  12.   
  13.             toast.show();  
  14.   
  15.         }  
  16.   
  17.     }  

使用小米3真机测试过之后,发现只会导致程序ANR,并不能实现设备重新启动.

第二种方法:

使用SU,改方法需要应用获取ROOT权限

http://stackoverflow.com/questions/5484535/runtime-exec-reboot-in-android

  1. public static void rebootSU() {  
  2.         Runtime runtime = Runtime.getRuntime();  
  3.         Process proc = null;  
  4.         OutputStreamWriter osw = null;  
  5.         StringBuilder sbstdOut = new StringBuilder();  
  6.         StringBuilder sbstdErr = new StringBuilder();  
  7.   
  8.         String command=”/system/bin/reboot”;  
  9.   
  10.         try { // Run Script  
  11.             proc = runtime.exec(”su”);  
  12.             osw = new OutputStreamWriter(proc.getOutputStream());  
  13.             osw.write(command);  
  14.             osw.flush();  
  15.             osw.close();  
  16.   
  17.         } catch (IOException ex) {  
  18.             ex.printStackTrace();  
  19.         } finally {  
  20.             if (osw != null) {  
  21.                 try {  
  22.                     osw.close();  
  23.                 } catch (IOException e) {  
  24.                     e.printStackTrace();                      
  25.                 }  
  26.             }  
  27.         }  
  28.         try {  
  29.             if (proc != null)  
  30.                 proc.waitFor();  
  31.         } catch (InterruptedException e) {  
  32.             e.printStackTrace();  
  33.         }  
  34.   
  35.         sbstdOut.append(new BufferedReader(new InputStreamReader(proc  
  36.                 .getInputStream())));  
  37.         sbstdErr.append(new BufferedReader(new InputStreamReader(proc  
  38.                 .getErrorStream())));  
  39.         if (proc.exitValue() != 0) {  
  40.         }  
  41.     }  

第三,原理同上,需要ROOT权限, runtime是用来执行linux  shell命令的,通过它可以实现对设备的相关操作

相关文章

  1. private void restart() {   
  2.         String cmd = ”su -c reboot”;  
  3.         //              String cmd = “su -c shutdown”;  
  4.         try {  
  5.             Runtime.getRuntime().exec(cmd);  
  6.         } catch (IOException e) {  
  7.             new AlertDialog.Builder(getApplicationContext()).setTitle(“Error”).setMessage(  
  8.                     e.getMessage()).setPositiveButton(”OK”null).show();  
  9.         }  
  10.     }  
 
 

第四种, 使用powerManger 来重启设备,同样的需要在配置文件中,添加权限 android.permission.REBOOT

http://developer.android.com/reference/android/os/PowerManager.html#reboot(java.lang.String)

猜你喜欢

转载自blog.csdn.net/qq_31939617/article/details/80707127
今日推荐