1. Are you ready for the Galaxy S20? Here is everything we know so far!

Reboot via code?

Discussion in 'Android Development' started by cruisx, May 16, 2011.

  1. cruisx

    cruisx Lurker
    Thread Starter

    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 (Text):
    1. try {
    2.     Runtime.getRuntime().exec("su");
    3.     Runtime.getRuntime().exec("reboot");
    4. } catch (IOException e) {
    5. }
    Code (Text):
    1. 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.
     



    1. Download the Forums for Android™ app!


      Download

       
  2. scary alien

    scary alien not really so scary
    VIP Member

    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).
     
  3. cruisx

    cruisx Lurker
    Thread Starter

    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 =)
     
  4. scary alien

    scary alien not really so scary
    VIP Member

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

    Cheers!
     
  5. scary alien

    scary alien not really so scary
    VIP Member

    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!
     
  6. cruisx

    cruisx Lurker
    Thread Starter

    Im going to try this now, so pretty much it should be
    Code (Text):
    1.  
    2.  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 (Text):
    1.  sbstdOut.append(ReadBufferedReader(new InputStreamReader(proc
    2.                 .getInputStream())));
    3.         sbstdErr.append(ReadBufferedReader(new InputStreamReader(proc
    4.                 .getErrorStream())));
    5.         if (proc.exitValue() != 0) {
    The above code is part of your link which i was previously using, any ideas why?

    Ill report back.
     
  7. scary alien

    scary alien not really so scary
    VIP Member

    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 (Text):
    1. try {
    2.     Runtime.getRuntime().exec("su");
    3.     Runtime.getRuntime().exec("reboot");
    4. } catch (IOException e) {
    5. }
    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!
     
  8. cruisx

    cruisx Lurker
    Thread Starter

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

    scary alien not really so scary
    VIP Member

    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 (Text):
    1. [FONT=Courier New]public static String runSUcommand (String command)[/FONT]
    2. [FONT=Courier New]{  [/FONT]
    3. [FONT=Courier New] final StringBuilder output = new StringBuilder();[/FONT]
    4. [FONT=Courier New] Process a;[/FONT]
    5.  
    6. [FONT=Courier New] BufferedReader read = null;[/FONT]
    7. [FONT=Courier New] try {[/FONT]
    8. [FONT=Courier New]    a = Runtime.getRuntime().exec("su");   // launch the shell (i.e., either su or sh)[/FONT]
    9. [FONT=Courier New]    DataOutputStream b = new DataOutputStream(a.getOutputStream());[/FONT]
    10. [FONT=Courier New]    b.writeBytes(command + "\n");          // send the command (\n is probably not needed if your command has it already)[/FONT]
    11. [FONT=Courier New]    read = new BufferedReader(new InputStreamReader(a.getInputStream()), 8192);[/FONT]
    12. [FONT=Courier New]    b.writeBytes("exit\n");                // exit the shell[/FONT]
    13. [FONT=Courier New]    b.flush();                             // flush the buffer[/FONT]
    14. [FONT=Courier New]    String line;[/FONT]
    15. [FONT=Courier New]    String separator = System.getProperty("line.separator");[/FONT]
    16.  
    17. [FONT=Courier New]    while ((line = read.readLine()) != null)   // read any output the command produced[/FONT]
    18. [FONT=Courier New]     output.append(line + separator);[/FONT]
    19.  
    20. [FONT=Courier New]    try[/FONT]
    21. [FONT=Courier New]    {[/FONT]
    22. [FONT=Courier New]     a.waitFor();[/FONT]
    23. [FONT=Courier New]     if (a.exitValue() == 255)                     // error occurred, exit value 255     [/FONT]
    24. [FONT=Courier New]      output.append("su/root command failed");       [/FONT]
    25. [FONT=Courier New]    }[/FONT]
    26. [FONT=Courier New]    catch (InterruptedException e)[/FONT]
    27. [FONT=Courier New]    {[/FONT]
    28. [FONT=Courier New]     output.append("su/root command failed ");     // SU command failed to execute[/FONT]
    29. [FONT=Courier New]    }[/FONT]
    30. [FONT=Courier New] } [/FONT]
    31. [FONT=Courier New] catch (IOException e)[/FONT]
    32. [FONT=Courier New] {[/FONT]
    33. [FONT=Courier New]  output.append("su/root command failed ");        // not rooted or su permissions not granted[/FONT]
    34. [FONT=Courier New] }[/FONT]
    35.  
    36. [FONT=Courier New]return output.toString();    // any residual return value from the command[/FONT]
    37. [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!
     
  10. cruisx

    cruisx Lurker
    Thread Starter

    Tried it also, still nothing. Just wondering do i have to give special permissions or something?
     
  11. scary alien

    scary alien not really so scary
    VIP Member

    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?
     
  12. cruisx

    cruisx Lurker
    Thread Starter


    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. :)
     
  13. scary alien

    scary alien not really so scary
    VIP Member

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

    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!
     
Loading...
Similar Threads - Reboot via code
  1. oneytlam
    Replies:
    2
    Views:
    256
  2. halfbeing
    Replies:
    5
    Views:
    395
  3. Roger Blair
    Replies:
    3
    Views:
    402
  4. crabocado
    Replies:
    8
    Views:
    686
  5. Rasil1127
    Replies:
    2
    Views:
    562
  6. Syahhhh3333
    Replies:
    5
    Views:
    583
  7. Akib Masud
    Replies:
    5
    Views:
    769
  8. Daiva Tam
    Replies:
    3
    Views:
    817
  9. a7474
    Replies:
    0
    Views:
    676
  10. benawhile
    Replies:
    3
    Views:
    566

Share This Page

Loading...