【问题标题】:Trying to reboot programmatically rooted Android device尝试以编程方式重启 Android 设备
【发布时间】:2015-03-08 01:13:39
【问题描述】:

我正在尝试以编程方式重新启动我的 Galaxy S3。

我尝试过的事情:

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

    try {
        Runtime.getRuntime().exec(new String[]{"su","-c","reboot now"});
    } catch (IOException e) {
        e.printStackTrace();
    }

    try 
    {           
        Process process = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(process.getOutputStream());
        os.writeBytes("reboot now\n");
    } 
    catch (Throwable t)
    {
        t.printStackTrace();
    }

你们知道如何做到这一点吗?

谢谢。

【问题讨论】:

  • 如果我尝试/system/bin/su,我会得到这个异常:java.io.IOException: Error running exec(). Command: [/system/bin/su, -c, reboot now] Working Directory: null Environment: null
  • 每次重启模拟器时都要输入"adb shell chmod 777 /dev/tty<>"(当然用串口设备替换/dev/tty")
  • @SagarPilkhwal,我没有使用模拟器。我使用的是 Galaxy S3 4.4.2

标签: android root


【解决方案1】:

尝试做一个看起来像《su \n reboot;》的普通字符串。 \n》而不是数组。 尝试从 shell 中获取答案,这对调试很有帮助。

你的 su 二进制文件的权限是什么?如果错了,你可以在《mount -o remount,rw /system》之后尝试《chmod 7777 /system/xbin/su》

这里是一些示例代码:(运行字符串命令,它是一个 \n 和 or ; 分隔的 linux shell 命令列表)

StringBuffer commandOutput = new StringBuffer();
    try {
        Process process = Runtime.getRuntime().exec("su\n");
        DataOutputStream out = new DataOutputStream(process.getOutputStream());
        out.writeBytes("export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/vendor/lib:/system/lib\n");
        out.writeBytes(command+"\n");
        out.flush();

        process.waitFor();
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        int numRead;
        char[] buffer = new char[1000];
        while ((numRead = in.read(buffer)) > 0) {
            commandOutput.append(buffer, 0, numRead);
        }
        in.close();
        process.waitFor();
    } catch ...

    return commandOutput.toString();

【讨论】:

  • 如何从 shell 中得到答案?谢谢!
  • 此代码返回此错误:W/System.err(26960): java.io.IOException: write failed: EPIPE (Broken pipe)
  • 这很奇怪,因为它适用于模拟器。另外,我的 Galaxy S3 是一个有根设备,所以我不知道为什么会出现这个错误。
  • 你检查su文件权限了吗?您是否安装了允许或拒绝 su 访问的超级用户 apk?如果卸载了呢? (APK)
【解决方案2】:

您可以使用PowerManager 使其重新启动(这并不能保证它会重新启动 - 操作系统可能会取消它): http://developer.android.com/reference/android/os/PowerManager.html#reboot(java.lang.String


http://developer.android.com/reference/android/Manifest.permission.html#REBOOT

【讨论】:

    猜你喜欢
    • 2011-06-02
    • 2016-01-04
    • 1970-01-01
    • 2011-08-09
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 2011-05-31
    相关资源
    最近更新 更多