android系统重启设备
2015-07-17 10:47
549 查看
第一种方法:
记得有一本书上介绍 说 0权限重启手机,原理是 在android 系统中,当显示一个toast,其实是将该toast挂载到窗体上, 而窗体又是系统的一个服务, 如果单位时间内不断地向窗体上挂载toast,就会不断的申请系统内存,导致系统重新启动。
使用小米3真机测试过之后,发现只会导致程序ANR,并不能实现设备重新启动.
第二种方法:
使用SU,改方法需要应用获取ROOT权限
http://stackoverflow.com/questions/5484535/runtime-exec-reboot-in-android
第三,原理同上,需要ROOT权限, runtime是用来执行linux shell命令的,通过它可以实现对设备的相关操作
相关文章
第四种, 使用powerManger 来重启设备,同样的需要在配置文件中,添加权限 android.permission.REBOOT
http://developer.android.com/reference/android/os/PowerManager.html#reboot(java.lang.String)
示例DEMO
记得有一本书上介绍 说 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 Script proc = 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 8 Reboot the device. Will not return if the reboot is successful. Requires the REBOOT permission. Parameters reason code to pass to the kernel (e.g., "recovery") to request special boot modes, or null.
示例DEMO
相关文章推荐
- Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)
- 应用开启动画界面的实现--Android
- android include 标签的使用
- Android 记录ListView滚动的位置的三种方法及判断是否滑动到底部
- android获得控件在屏幕中的绝对坐标 getLocationInWindow 和 getLocationOnScreen
- Android studio 打jar包
- Android--混淆
- Android问题集锦
- Android ViewDragHelper完全解析 自定义ViewGroup神器
- Android onTouch事件与手势操作
- Android中listView分组显示内容和下拉刷新
- android 获取屏幕宽高 & 控件宽高
- Android4.4 Launcher3分析之IconCache
- Android运行时ART简要介绍和学习计划
- Android应用如何监听自己是否被卸载及卸载反馈功能的实现(第二版)
- Android应用资源系列之属性(Attribute)资源
- Android报错记录2
- Android APK 手动命令编译、打包、签名步骤
- android 自定义控件 总结
- Android应用如何监听自己是否被卸载及卸载反馈功能的实现