Runtime.exec() : Reboot in Android?
Asked Answered
E

5

18

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.

Ekaterinodar answered 30/3, 2011 at 9:33 Comment(2)
I already answered this a couple of months ago: #4580754 , #3456967Brion
Following this question, I made a small app that I open sourced: github.com/rbochet/Fast-Forward-RebootEkaterinodar
M
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) {
                }
        }
Mickeymicki answered 7/4, 2011 at 16:10 Comment(3)
Thanks for the full code, I didn't use Processes in the good way.Ekaterinodar
Cannot run program "su": error=13, Permission deniedVareck
@GauravMandlik did you find any thing on this .Aquacade
T
36

Finally after weeks of searching:

Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c","reboot now"});
Towelling answered 24/4, 2011 at 6:5 Comment(7)
Thanks, i like this compact one !Ekaterinodar
For some reason I had to use: Runtime.getRuntime().exec(new String[]{"su","-c","reboot now"}); instead, but it worked. Thank you so much!Strontian
It is not working in my samsung device, do i need to give any permission ?Granjon
@DavidShellabarger: It's because su might be under /system/xbin/ instead of /system/bin/. The location depends on how you got su or how you rooted your device.Chamomile
xbin worked thanks! Also shouldn't an unqualified "su" call work fine as well since the path should be already set? I was using 'su' fine from terminal emulator without a fully qualified path.Confiscate
Cannot run program "su": error=13, Permission deniedVareck
The device in question needs to be rooted.Towelling
M
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) {
                }
        }
Mickeymicki answered 7/4, 2011 at 16:10 Comment(3)
Thanks for the full code, I didn't use Processes in the good way.Ekaterinodar
Cannot run program "su": error=13, Permission deniedVareck
@GauravMandlik did you find any thing on this .Aquacade
L
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.

Luganda answered 11/5, 2012 at 17:29 Comment(1)
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"Subadar
W
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;}
}
Walls answered 17/7, 2014 at 7:7 Comment(1)
Cannot run program "su": error=13, Permission deniedVareck
T
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.

Tupler answered 11/8, 2016 at 7:28 Comment(2)
Runtime.getRuntime().exec(new String[]{"su","-c","reboot now"}); seems to work for both casesEngulf
Cannot run program "su": error=13, Permission deniedVareck

© 2022 - 2024 — McMap. All rights reserved.