Reboot the phone on a button click

ε祈祈猫儿з 提交于 2019-11-26 17:47:13

问题


I am making an Android app that needs to have the phone reboot or turn off when a button is clicked. Is this possible? Or will the phone require root access?


回答1:


You can do that using android.os.PowerManager. Function reboot(String reason) is available, you need permission:

android.permission.REBOOT

Official site:

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

Of course, you are likely to get that permission only if your application is signed with the system signing key:

How to compile Android Application with system permissions




回答2:


I did this in my app by calling the method below.

Notes: 1. Make sure the phone is rooted 2. Allow the app in your root manager to automatically grant root permission.

    void reboot() {
        if (reboot) {

          try {
              Process proc = Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "reboot"});
              proc.waitFor();
          } catch (Exception ex) {
              Log.e(TAG, "Error ", ex);
          }

        }
  }



回答3:


If your device is rooted device then you can use below code.

Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "reboot now"});


来源:https://stackoverflow.com/questions/4966486/reboot-the-phone-on-a-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!