Runtime.exec() : Reboot in Android?

后端 未结 5 1113
慢半拍i
慢半拍i 2020-11-28 07:41

I\'m looking for a solution which can be used to reboot a rooted device. I jknow that rebooting a device is very poor design for the user, as stated here, and it\'s not real

相关标签:
5条回答
  • 2020-11-28 08:04

    I find I can not do a reboot programatically.

    In addition, I can open a terminal window on my android phone using Terminal Emulator app, type su get the # prompt for root access and then type "#reboot" and I get the response "not permitted!"

    Any suggestions?

    OK, nevermind, I figured it out. On HTC phones the reboot command will not work even with SU root access. Need to invoke BUSYBOX to perform the reboot command.

    0 讨论(0)
  • 2020-11-28 08:19

    After long struggle i found working solution.

    If your system is used serial port then execute below command,

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

    if use normal port then excure below command

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

    difference is only /bin and /xbin

    so can code like if first command throw exception then execute second.

    0 讨论(0)
  • 2020-11-28 08:22

    reboot works fine in android. you are probably not doing runtime.exec() properly. you need to handle the

        public static void rebootSU() {
        Runtime runtime = Runtime.getRuntime();
        Process proc = null;
        OutputStreamWriter osw = null;
        StringBuilder sbstdOut = new StringBuilder();
        StringBuilder sbstdErr = new StringBuilder();
    
        String command="/system/bin/reboot";
    
        try { // Run Script
    
            proc = runtime.exec("su");
            osw = new OutputStreamWriter(proc.getOutputStream());
                                osw.write(command);
                    osw.flush();
            osw.close();
    
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (osw != null) {
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();                    
                }
            }
        }
        try {
            if (proc != null)
                proc.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        sbstdOut.append(ReadBufferedReader(new InputStreamReader(proc
                .getInputStream())));
        sbstdErr.append(ReadBufferedReader(new InputStreamReader(proc
                .getErrorStream())));
        if (proc.exitValue() != 0) {
                    }
            }
    
    0 讨论(0)
  • 2020-11-28 08:25

    Finally after weeks of searching:

    Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c","reboot now"});
    
    0 讨论(0)
  • 2020-11-28 08:26

    This code works on Samsung Galaxy S2, S4, Sony Xperia S (LTi 26)

    public static void rebootDevice()
    {
        try 
        {           
            Process process = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(process.getOutputStream());
            os.writeBytes("reboot \n");
        } 
        catch (Throwable t) {t.printStackTrace;}
    }
    
    0 讨论(0)
提交回复
热议问题