• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Reboot via code?

cruisx

Lurker
Hey Im trying to implement a reboot in my app for root users but the code below does not work. The SU prompt pops up but when i allow the action nothing happens, it will not reboot. Any ideas why?


Code:
try {
    Runtime.getRuntime().exec("su");
    Runtime.getRuntime().exec("reboot");
} catch (IOException e) {
}

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

I am also hearing that the regular sdk cannot make reboot applications or something like that. If anyone can help me out id appreciate it.
 
cruisx,

Your code looks similar to what is mentioned in this link:

java - Runtime.exec() : Reboot in Android ? - Stack Overflow

which should work.

I've done something similar in an app I've written for my own personal use, but I don't recall using the "-c" option. I'll have to check my code when I get home tonight to confirm.

By the way, just to make sure...you are trying this on a rooted phone, right? (your comment about "The SU prompt pops up" makes me think that you are, but I just wanted to make sure).

Cheers!

(I'll reply back tonight after I look at my code if you still need).
 
Yes rooted phone, I have tried many diff ways to do it but i cant undeerstand why an app like quick boot will reboot my phone but my code wont, even though the SU promt seems to be running the same "reboot" command.

Please let me know if you find anything =)
 
Yes rooted phone, I have tried many diff ways to do it but i cant undeerstand why an app like quick boot will reboot my phone but my code wont, even though the SU promt seems to be running the same "reboot" command.

Please let me know if you find anything =)

No problem... I'll make a note and re-post tonight if you want.

Cheers!
 
cruisx,

Here's what my app does (that I originally wrote for my rooted Droid X):

I basically have a class like the rebootSU one listed in the thread I posted earlier:

java - Runtime.exec() : Reboot in Android ? - Stack Overflow

but the major difference is that I pass the command as a String to the rebootSU function with a value of "reboot -p\n" which works just fine on my Droid X (I'm wondering if your issue is a missing newline character?).

Hope that helps!

Cheers!
 
cruisx,

Here's what my app does (that I originally wrote for my rooted Droid X):

I basically have a class like the rebootSU one listed in the thread I posted earlier:

java - Runtime.exec() : Reboot in Android ? - Stack Overflow

but the major difference is that I pass the command as a String to the rebootSU function with a value of "reboot -p\n" which works just fine on my Droid X (I'm wondering if your issue is a missing newline character?).

Hope that helps!

Cheers!

Im going to try this now, so pretty much it should be
Code:
 String command="/system/bin/reboot -p\n";

Also before I try this, last night I get the following error.

The method ReadBufferedReader(InputStreamReader) is undefined for the type MainActivity

Code:
 sbstdOut.append(ReadBufferedReader(new InputStreamReader(proc
                .getInputStream())));
        sbstdErr.append(ReadBufferedReader(new InputStreamReader(proc
                .getErrorStream())));
        if (proc.exitValue() != 0) {

The above code is part of your link which i was previously using, any ideas why?

Ill report back.
 
I don't know about the error...my code is very similar to the StackOverflow link, but it uses slightly different I/O commands.

There was another link on that page, Android 2.2: Reboot device programmatically - Stack Overflow, that showed this:
Code:
try {
    Runtime.getRuntime().exec("su");
    Runtime.getRuntime().exec("reboot");
} catch (IOException e) {
}
that will supposedly work.

Also, there was a link to that poster's open source reboot app:

https://github.com/rbochet/Fast-Forward-Reboot

Cheers!
 
I don't know about the error...my code is very similar to the StackOverflow link, but it uses slightly different I/O commands.

There was another link on that page, Android 2.2: Reboot device programmatically - Stack Overflow, that showed this:
Code:
try {
    Runtime.getRuntime().exec("su");
    Runtime.getRuntime().exec("reboot");
} catch (IOException e) {
}
that will supposedly work.

Also, there was a link to that poster's open source reboot app:

https://github.com/rbochet/Fast-Forward-Reboot

Cheers!

Yes thanks, I tried all that yesterday, even the source, nothing works LOL. Oh well thanks for helping :cool:
 
cruisx,

I took my code and cleaned it up a bit (I had to remove my app-specifc logging, so I hope I didn't break anything, but this is what I use), but try this:
Code:
[FONT=Courier New]public static String runSUcommand (String command)[/FONT]
[FONT=Courier New]{  [/FONT]
[FONT=Courier New] final StringBuilder output = new StringBuilder();[/FONT]
[FONT=Courier New] Process a;[/FONT]
 
[FONT=Courier New] BufferedReader read = null;[/FONT]
[FONT=Courier New] try {[/FONT]
[FONT=Courier New]    a = Runtime.getRuntime().exec("su");   // launch the shell (i.e., either su or sh)[/FONT]
[FONT=Courier New]    DataOutputStream b = new DataOutputStream(a.getOutputStream());[/FONT]
[FONT=Courier New]    b.writeBytes(command + "\n");          // send the command (\n is probably not needed if your command has it already)[/FONT]
[FONT=Courier New]    read = new BufferedReader(new InputStreamReader(a.getInputStream()), 8192);[/FONT]
[FONT=Courier New]    b.writeBytes("exit\n");                // exit the shell[/FONT]
[FONT=Courier New]    b.flush();                             // flush the buffer[/FONT]
[FONT=Courier New]    String line;[/FONT]
[FONT=Courier New]    String separator = System.getProperty("line.separator");[/FONT]
 
[FONT=Courier New]    while ((line = read.readLine()) != null)   // read any output the command produced[/FONT]
[FONT=Courier New]     output.append(line + separator);[/FONT]
 
[FONT=Courier New]    try[/FONT]
[FONT=Courier New]    {[/FONT]
[FONT=Courier New]     a.waitFor();[/FONT]
[FONT=Courier New]     if (a.exitValue() == 255)                     // error occurred, exit value 255     [/FONT]
[FONT=Courier New]      output.append("su/root command failed");       [/FONT]
[FONT=Courier New]    }[/FONT]
[FONT=Courier New]    catch (InterruptedException e)[/FONT]
[FONT=Courier New]    {[/FONT]
[FONT=Courier New]     output.append("su/root command failed ");     // SU command failed to execute[/FONT]
[FONT=Courier New]    }[/FONT]
[FONT=Courier New] } [/FONT]
[FONT=Courier New] catch (IOException e)[/FONT]
[FONT=Courier New] {[/FONT]
[FONT=Courier New]  output.append("su/root command failed ");        // not rooted or su permissions not granted[/FONT]
[FONT=Courier New] }[/FONT]
 
[FONT=Courier New]return output.toString();    // any residual return value from the command[/FONT]
[FONT=Courier New]}[/FONT]
And then invoke the command with something like this:

String SUcmdresult = Common.runSUcommand("reboot\n");

If you have trouble with the reboot command, try testing with some other command first (they don't have to be root-related commands since su will act just like a shell).

Hope that helps.

Cheers!
 
Tried it also, still nothing. Just wondering do i have to give special permissions or something?

Well, you'll have to tell the SuperUser app that you want to allow su/root permissions...you're getting the SuperUser whitelist app's prompt asking you for permissions, right?
 
Well, you'll have to tell the SuperUser app that you want to allow su/root permissions...you're getting the SuperUser whitelist app's prompt asking you for permissions, right?


Ya I am, its ok LOL you've helped me enough. I guess ill just drop the feature untill i can figure out why its not working >_<. Ill try it on my friends phone next week.

Thanks for all the help though. :)
 
Ya I am, its ok LOL you've helped me enough. I guess ill just drop the feature untill i can figure out why its not working >_<. Ill try it on my friends phone next week.

Thanks for all the help though. :)

No problem. Sorry you're having troubles...:confused:

But you might want to try some non-root commands to see if the basic functionality works or not.

You can change the "su" reference to "sh" and send some commands through.

Good luck!
 
Back
Top Bottom