Android 2.2: Reboot device programmatically

2019-01-09 03:30发布

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

6条回答
一纸荒年 Trace。
2楼-- · 2019-01-09 04:18

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

查看更多
ゆ 、 Hurt°
3楼-- · 2019-01-09 04:19

I am using Xamarin. For me the solution is:

Java.Lang.Runtime.GetRuntime().Exec(new String[] { "/system/xbin/su", "-c", "reboot now" });
查看更多
Root(大扎)
4楼-- · 2019-01-09 04:24

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.
    }
}
查看更多
放荡不羁爱自由
5楼-- · 2019-01-09 04:27

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);
    }
查看更多
看我几分像从前
6楼-- · 2019-01-09 04:27

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.

查看更多
ら.Afraid
7楼-- · 2019-01-09 04:29

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 )
查看更多
登录 后发表回答