【问题标题】:Restarting a device programmatically以编程方式重启设备
【发布时间】:2016-01-04 05:58:58
【问题描述】:

在我的 android 应用程序中,我想在单击按钮时重新启动我的 android 设备。 但它不起作用。
到目前为止,我已经这样做了。

ImageButton restartmob = (ImageButton) this.findViewById(R.id.restartmob);
    restartmob.setOnClickListener(new ImageButton.OnClickListener() {
        @Override
        public void onClick(View v) {
            Process proc = null;            
            try {
               //proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "reboot" });
                 proc = Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c","reboot now"});
                proc.waitFor();
            } catch (Exception ex) {
                Log.i(TAG, "Could not reboot", ex);
            }
        }
    });

我还在清单文件中添加了以下权限。

<permission android:name="android.permission.REBOOT"/>

当我点击图片按钮重启时,出现以下异常。

java.io.IOException: Error running exec(). Command: [/system/bin/su, -c, reboot now] 
Working Directory: null Environment: null   
at java.lang.ProcessManager.exec(ProcessManager.java:211)   
at java.lang.Runtime.exec(Runtime.java:173)
at java.lang.Runtime.exec(Runtime.java:128)
at com.android.onlinepayment.activity.SystemSubMenuActivity$1.onClick(SystemSubMenuActivity.java:49)
at android.view.View.performClick(View.java:5184)   
at android.view.View$PerformClick.run(View.java:20910)  
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)    
at java.lang.reflect.Method.invoke(Native Method)   
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388  
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
Caused by: java.io.IOException: No such file or directory   
at java.lang.ProcessManager.exec(Native Method)
at java.lang.ProcessManager.exec(ProcessManager.java:209)
... 13 more

我的代码有什么问题?请帮助我。
对于信息,我正在测试 android Lollipop(如果重要的话)。
提前致谢。

【问题讨论】:

标签: android reboot


【解决方案1】:

您需要的权限与您的重启方法无关,因为您的方法需要有根手机(使用su)。要重启手机,需要您的权限,但请致电PowerManager#reboot

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
pm.reboot(null);

【讨论】:

  • 我得到了这个异常:java.lang.SecurityException:用户 10695 和当前进程都没有 android.permission.REBOOT。
  • 好像只有你的应用是系统应用权限才生效,看这里:stackoverflow.com/questions/3635101/…
  • 这在我的情况下也不起作用,并且得到与@Suniel 相同的异常。
  • 如果我有一个 root 手机,具有 root 访问权限的应用程序,并安装为系统,它给出了与 Suniel 相同的错误? (java.lang.SecurityException:用户 [不记得] 和当前进程都没有 android.permission.REBOOT)。这里说 protectionLevel 是签名|特权:github.com/aosp-mirror/platform_frameworks_base/blob/…。我应该是最后一个。我也在 Lollipop 5.1 上这样做,不应该是 Mashmallow 运行时权限请求。
【解决方案2】:

在 API 24 上,如果您的应用是设备所有者应用,您可以调用:devicePolicyManager.reboot(yourAdminComponent)

请参阅文档here

【讨论】:

  • 什么是你的AdminComponent?如何添加管理组件?
  • @AAA AdminComponent 是在您成为设备所有者时设置的 ComponentName。看起来像“com.mycompany.product/.DeviceOwner”,它引用了一个类名。
【解决方案3】:

试试这个...

try {
      Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "reboot" });
      proc.waitFor();
         } catch (Exception ex) {
                Log.i(TAG, "Could not reboot", ex);
      }

添加权限重启

【讨论】:

  • 这是否依赖于安卓版本?其实我正在测试 android 5.0.1
  • 无需添加权限重启,因为您无法在您的应用中获得此权限,只需使用任何 root 权限管理器,然后默认允许您的应用进行 root 访问。
【解决方案4】:

您无法从普通的 SDK 应用程序重新启动。只有使用系统固件签名密钥签名的应用程序才能执行此操作。 从这个答案复制,

Programmatically switching off Android phone

您需要系统密钥来为您的应用签名。详情见此帖;

How to compile Android Application with system permissions

【讨论】:

    【解决方案5】:

    经过长时间的努力,我找到了可行的解决方案。

    如果你的系统使用的是串口,那么执行下面的命令,

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

    如果使用普通端口,则执行以下命令

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

    区别只是/bin/xbin

    所以可以像第一个命令抛出异常然后执行第二个一样编写代码。

    【讨论】:

    • 这在我的情况下不起作用。它给了我一个例外,没有找到这样的目录或文件。我错过了什么吗?
    【解决方案6】:

    您可以使用传说中的LibSuperUser 库。

    编写这个简单的代码:

    Shell.SU.run("reboot");
    

    【讨论】:

      【解决方案7】:

      你正在做&lt;permission ... /&gt;。 你需要做&lt;USES-permission ... /&gt;

      不需要大写,只是为了强调它。

      【讨论】:

        【解决方案8】:
        try {//   It worked for me on Android 7.0
                    Runtime.getRuntime().exec(arrayOf("/system/bin/su", "-c", "reboot now"))
                }catch (e:Exception){
                    Log.e(TAG,e.message)
                    e.printStackTrace()
                }
        

        【讨论】:

          【解决方案9】:

          试试这个代码 -

          第一个导入包

          import com.google.android.things.device.DeviceManager;
          

          创建函数并添加一些异常

          public void StartReboot() throws IllegalStateException, RuntimeException, SecurityException {
          

          创建对象

                try {
                   DeviceManager deviceManager = DeviceManager.getInstance();
          

          开始重启

                   deviceManager.reboot();
          

          捕捉异常

                } catch(IllegalStateException ex) {
                  OnError("Underlying system service is not ready.");
                } catch(RuntimeException ex) {
                  OnError("Underlying system service encountered an error.");
                } catch(SecurityException ex) {
                  OnError("Calling context does not have com.google.android.things.permission.REBOOT permission");
                }
              }
          

          【讨论】:

          • 虽然这可能会回答这个问题,但如果可能的话,您应该 edit 您的回答包括一个简短的解释如何这个代码块回答这个问题。这有助于提供上下文,并使您的答案对未来的读者更有用。
          • 嘿@Hoppeduppeanut 我会尽快添加一个简短的解释
          • @Hoppeduppeanut 现在看看是否正确?
          【解决方案10】:

          其他答案未考虑 DevicePolicyManager API。也许您可以将您的应用设置为设备管理员http://blogs.quovantis.com/android-device-administration-apis/(用户需要专门从设置中进行设置)并且有一个重启方法https://developer.android.com/reference/android/app/admin/DevicePolicyManager#reboot(android.content.ComponentName)

          所以我怀疑是否需要 root 才能做到这一点。我正在编写应用程序来测试它(尝试使从计算机远程重启成为可能)。

          【讨论】:

            猜你喜欢
            • 2011-06-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-03-09
            • 2011-03-30
            • 2011-08-09
            • 1970-01-01
            相关资源
            最近更新 更多