尝试以编程方式重新启动root设备

时间:2014-09-10 19:06:34

标签: android root

我尝试以编程方式重启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();
    }

你们有任何想法如何实现这个目标吗?

感谢。

2 个答案:

答案 0 :(得分:0)

尝试做一个正常的字符串,看起来像“su \ n reboot; \ n“而不是数组。 尝试从shell中获得答案,这有助于调试。

你的su二进制文件有哪些权限?如果他们错了,你可以在“mount -o remount,rw / system”之后尝试“chmod 7777 / system / xbin / su”

下面是一些示例代码:(运行string命令,这是一个\ n和\或;分离的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();

答案 1 :(得分:0)

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


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