android - 无法使用runtime.exec重新启动设备

标签 android exec reboot

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

有人知道为什么吗? su 可以正常工作, super 用户应用程序也可见。我应该注意到其他各种 shell 命令也可以工作,例如 netcfg(chmod' 到 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;
    }

最佳答案

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

最好在应用程序中测试所有三个场景。

关于android - 无法使用runtime.exec重新启动设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5603221/

相关文章:

android - 重启后如何重新设置闹钟

ios - 如何获取 iOS 重启时间?

java - 在新的月份更新变量不会失败

android - 如何使用改造从 GET url 获得响应

android - 彩信发件人地址问题

javascript - PhoneGap 未发出 AJAX (jsonp) 请求

c - execl 调用奇怪的行为

linux - bash,如何将记录器脚本转换为函数

c - 如何在 execlp() 之后找到程序的返回值?

java - 运行时.exec() : Reboot in Android?