2

出于某种原因,我无法使用 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;
    }
4

5 回答 5

2

根据您在设备上获得 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"});

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

于 2013-02-05T08:36:23.883 回答
1

终于经过数周的搜索:

Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c","reboot now"});
于 2011-04-24T18:53:50.673 回答
0

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

于 2011-04-11T19:58:32.603 回答
0

如果您想按下按钮,请尝试:

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。

于 2011-05-08T03:05:36.617 回答
0

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

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

于 2011-11-07T03:57:45.017 回答