14

I would like to know if there is a way to reboot the device through code. Ive tried:

Intent i = new Intent(Intent.ACTION_REBOOT); 
i.putExtra("nowait", 1); 
i.putExtra("interval", 1); 
i.putExtra("window", 0); 
sendBroadcast(i);

And added permissions for REBOOT but it still doesnt work.

Thanks

ismail
  • 46,010
  • 9
  • 86
  • 95
Johan
  • 427
  • 2
  • 7
  • 14
  • 1
    I think I need to sign my app with the "platform certificate". Can anyone tell me how to do this? Im not planning on releasing this app on the market, I just need it for my android tablet. Thanks – Johan Jan 02 '11 at 20:46
  • 14
    If it's an HTC Desire, you can turn on the GPS and do some 3D rendering with OpenGL. The combination will cause the phone to quickly overheat which in turn causes a reboot. – Graham Borland Nov 12 '11 at 00:03
  • Through experience, an Android 2.2 (or 2.3) device will spontaneously reboot if there are enough pending intents queued up that you aren't servicing (for instance, if your thread to process intents is blocked on something else). Not that this in any way is really an *acceptable* way to reboot the device. – Michael Apr 28 '14 at 21:21
  • Is there any way to programmatically reboot the device without rooting it? – Bogdan Doicin Sep 16 '14 at 08:52
  • Works for me on CM12.1 Samsung 4 Jactivelte phone. – portsample Jan 21 '16 at 05:25

6 Answers6

35

This seemed to work for me:

try {
        Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "reboot" });
        proc.waitFor();
    } catch (Exception ex) {
        Log.i(TAG, "Could not reboot", ex);
    }
saulpower
  • 1,258
  • 1
  • 10
  • 13
0

Still for rooted devices, but in case you want safer (process.waitFor() is conditioned, in separate try-catch, we have proper exception handling, "now" added in command after reboot, which is necessary for some devices, etc.) and maybe cleaner code, take a look at this:

Process rebootProcess = null;
try
{
    rebootProcess = Runtime.getRuntime().exec("su -c reboot now");
}
catch (IOException e)
{
    // Handle I/O exception.
}

// We waitFor only if we've got the process.
if (rebootProcess != null)
{
    try
    {
        rebootProcess.waitFor();
    }
    catch (InterruptedException e)
    {
        // Now handle this exception.
    }
}
Budimir Grom
  • 756
  • 6
  • 12
0

You could possibly use the PowerManager to make it reboot (this does not guarantee that it'll reboot - OS may cancel it): links
link #2

Muhammad Waleed
  • 2,517
  • 4
  • 27
  • 75
0

I am using Xamarin. For me the solution is:

Java.Lang.Runtime.GetRuntime().Exec(new String[] { "/system/xbin/su", "-c", "reboot now" });
Martin Evans
  • 45,791
  • 17
  • 81
  • 97
-1

Here is a solution. Remember, the device must be rooted.

try{
    Process p = Runtime.getRuntime().exec("su");
    OutputStream os = p.getOutputStream();                                       
    os.write("reboot\n\r".getBytes());
    os.flush();
}catch(IOException )
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
antraxa
  • 87
  • 3
-4

If the phone is rooted, it's actually very simple:

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

The first command will ask for superuser permission. The second, will reboot the phone. There is no need for extra permissions in the manifest file since the actual rebooting is handled by the executed comamand, not the app.

Aleadam
  • 40,203
  • 9
  • 86
  • 108
  • I tried this, it asked for super user permission, a toast message has been shown saying `application has been granted super user permission`, then no exception caught, but phone not rebooted. – Mithun Sreedharan Oct 04 '11 at 06:12
  • 3
    This won't work because second command will run in a different process than the `su` process. – Caner Aug 16 '12 at 13:00
  • 9
    answer is not useful because, "Runtime.getRuntime().exec("your command");" is always run in different session. so "reboot" command after "su" will not run under "su" permission. So you have to use this "Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c","reboot now"}); ", this is works for me. – VISHAL VIRADIA Nov 11 '12 at 08:30
  • 1
    Like Vishal says, each exec() call is run in it's own independent shell. Just 'su' will wait for input, so if you had called waitFor(), like in the correct answer, your app would just stall (I think) – devin Mar 11 '13 at 16:00