Android 2.2: Reboot device programmatically
Asked Answered
C

6

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

Clarkin answered 2/1, 2011 at 20:8 Comment(5)
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. ThanksClarkin
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.Husein
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.Quinquagesima
Is there any way to programmatically reboot the device without rooting it?Backstroke
Works for me on CM12.1 Samsung 4 Jactivelte phone.Sandhi
C
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);
    }
Cown answered 11/11, 2011 at 23:50 Comment(1)
I am getting this Could not rebootjava.io.IOException: Cannot run program "su": error=13, Permission deniedDifficult
U
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.
    }
}
Ulna answered 18/9, 2014 at 17:16 Comment(0)
K
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

Kristof answered 7/3, 2015 at 16:39 Comment(1)
Please add the useful code along with the links in case the links break over time.Et
B
0

I am using Xamarin. For me the solution is:

Java.Lang.Runtime.GetRuntime().Exec(new String[] { "/system/xbin/su", "-c", "reboot now" });
Bedaub answered 27/9, 2017 at 14:57 Comment(0)
C
-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 )
Czerny answered 26/10, 2012 at 22:59 Comment(0)
E
-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.

Epigoni answered 23/1, 2011 at 7:43 Comment(4)
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.Rankins
This won't work because second command will run in a different process than the su process.Charcuterie
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.Kilah
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)Amphictyony

© 2022 - 2024 — McMap. All rights reserved.