【问题标题】:Can't reboot device using runtime.exec无法使用 runtime.exec 重新启动设备
【发布时间】:2011-04-09 06:08:46
【问题描述】:

由于某种原因,我无法使用 Runtime.getRuntime().exec("/system/bin/reboot"); 重新启动 Android 设备。我现在已经在 3 台设备上尝试了以下代码,但没有运气。一个是从 rowboat-android 源构建的。另外两个是摩托罗拉 Droid Pro(Rooted,股票)和 HTC Ledgent(Rooted,Cynogen Mod)。所有设备均运行 Android 2.2 Froyo。

有人知道为什么吗? su 工作以及超级用户应用程序是可见的。我应该注意到其他各种 shell 命令确实有效,例如 netcfg (chmod' to 777) 和 ls。

public static boolean executeCommand(SHELL_CMD iShellCmd){
        String cmd = null;
        String line = null;
        String fullResponse = null;
        Process lPs = null;

        Log.d(Global.TAG,"--> !!!!! Running shell command");

        if (iShellCmd == SHELL_CMD.restart_device){
            cmd = "reboot";
        }

        Log.d(Global.TAG,"--> About to exec: " + cmd);

        try {
            lPs = Runtime.getRuntime().exec("su");
            lPs = Runtime.getRuntime().exec("/system/bin/reboot");
        } catch (Exception e) {
            e.printStackTrace();
        }

        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(lPs.getOutputStream()));
        BufferedReader in = new BufferedReader(new InputStreamReader(lPs.getInputStream()));

        try {
            while ((line = in.readLine()) != null) {
                Log.d(Global.TAG,"--> Command result line: " + line);
                if (fullResponse == null){
                    fullResponse = line;
                }else{
                    fullResponse = fullResponse + "\n" + line;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        Log.d(Global.TAG,"--> Full response was: " + fullResponse);

        return true;
    }

【问题讨论】:

  • 如果您从终端模拟器运行该命令是否有效?你试过reboot而不是/system/bin/reboot吗?
  • 调用“重新启动”不起作用并锁定应用程序并显示 ANR(强制退出)。如果我使用 adb shell 在设备上调用 reboot ,系统会正确重启。然后我需要重新启动系统才能再次使用 runtime.exec 发出 shell 命令。
  • 强制退出后的logcat是什么?搜索单词FATAL之后的任何内容

标签: android exec reboot


【解决方案1】:

根据您在设备上获得 root 权限的方式,您可以执行以下任何操作:

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

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

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

在您的应用程序中测试所有三个场景可能会更好。

【讨论】:

    【解决方案2】:

    经过数周的搜索终于:

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

    【讨论】:

      【解决方案3】:

      尝试在不同的行上运行“su /system/bin/reboot”而不是 su 和命令。这可能会有所帮助:)

      【讨论】:

      • 你的意思是 .exec("su\n/system/bin/reboot");还是你的意思是 .exec("su /system/bin/reboot");
      【解决方案4】:

      如果你想按下按钮试试:

      final Button button = (Button) findViewById(R.id.button1);
          button.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  // Perform action on click
                   try {
                       Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c","reboot"});              
                  } catch (IOException e) {
                  }               
              }
          });
      

      顺便说一下,为了让这段代码工作,按钮必须被称为 button1。

      【讨论】:

        【解决方案5】:

        我将"/system/bin/su" 部分更改为"su" 而不是{"/system/bin/su","-c","reboot"},然后它对我有用。

        点赞Runtime.getRuntime().exec(new String[]{"su","-c","reboot"});

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-07-25
          • 1970-01-01
          • 2014-05-05
          • 1970-01-01
          • 1970-01-01
          • 2016-03-05
          • 2013-09-09
          • 1970-01-01
          相关资源
          最近更新 更多