Android reboot

android 重启手机代码

Android重启手机代码分析


第一种方法:

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

[java]  view plain copy
  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

[java]  view plain copy
  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命令的,通过它可以实现对设备的相关操作

相关文章

[java]  view plain copy
  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)


[html]  view plain copy
  1. public void reboot (String reason)  
  2.   
  3. Added in API level 8  
  4. Reboot the device. Will not return if the reboot is successful.  
  5.   
  6. Requires the REBOOT permission.  
  7.   
  8. Parameters  
  9. reason  code to pass to the kernel (e.g., "recovery") to request special boot modes, or null.  


示例DEMO



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值