17

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 really for an application. The main purpose is to reboot the phone during my tests (I work on a video chat application, and sometimes I need to reboot when everything goes south)

I observed that rebooting a phone is far far quicker using reboot in a terminal (adb shell or ConnectBot for instance) than going through the usual of rebooting with the ACTION_REBOOT, that I can't use anyway.

For the moment, I'm able to get the superuser privileges, with

Process root = Runtime.getRuntime().exec("su");

But I can't do the actual reboot. I try on a G1 (HTC) and on a Galaxy S (Samsung) without any success. I located the reboot executable in /system/bin/reboot

Here are some of my attempts :

Process reboot = Runtime.getRuntime().exec("/system/bin/reboot");
Process reboot = Runtime.getRuntime().exec("reboot");
Process reboot = Runtime.getRuntime().exec("su reboot"); 

I read this article about the pitfalls of Runtime.exec(), but I think I'm not in this case.

As using ConnectBot enable me to do such an action, I'm pretty sure it's possible. Please don't tell me to go and have a look to the ConnectBot code, it's a big and complicated project :)

Can you help me with this issue ?

Thanks.

Community
  • 1
  • 1
Rob
  • 2,736
  • 5
  • 29
  • 38
  • I already answered this a couple of months ago: http://stackoverflow.com/questions/4580254/android-2-2-reboot-device-programmatically/4772796#4772796 , http://stackoverflow.com/questions/3456467/why-does-my-app-throw-an-android-permission-reboot-securityexception/4772820#4772820 – Aleadam Apr 24 '11 at 07:31
  • Following this question, I made a small app that I open sourced: https://github.com/rbochet/Fast-Forward-Reboot – Rob May 06 '11 at 04:56

5 Answers5

36

Finally after weeks of searching:

Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c","reboot now"});
Kevin Parker
  • 15,836
  • 18
  • 75
  • 100
16

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) {
                }
        }
jkhouw1
  • 7,232
  • 3
  • 30
  • 24
1

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.

howard
  • 41
  • 4
  • If anyone is stuck with the same issue on HTC phones, there is an easier way to bypass that "not permitted!" message, without having to install Busybox : instead of invoking the "reboot" command, just write a "b" into /proc/sysrq-trigger by calling "**echo b > /proc/sysrq-trigger**" – Lyrkan Jul 19 '13 at 08:09
1

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;}
}
XXX
  • 8,662
  • 7
  • 42
  • 52
0

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.

Yogesh Rathi
  • 5,684
  • 4
  • 42
  • 71