Search code examples
androidrootshutdown

Android root poweroff


On my android app for root user, I perform a shutdown with

su reboot -p

It works ok, but I noticed that the phone is shutdown almost instantly (as opposed from showing the shutdown animation, and probably doing other stuff).

Is this the correct way to shutdown an android phone programmatically? Are there some critical code that requires to be executed before shutdown?


Solution

  • In the Android source code, you can find the following:

    /**
     * Low-level function turn the device off immediately, without trying
     * to be clean.  Most people should use
     * {@link android.internal.app.ShutdownThread} for a clean shutdown.
     *
     * @deprecated
     * @hide
     */
    @Deprecated
    public static native void shutdown();
    

    I think that this native function corresponds to your su reboot -p. Moreover, you can see from the quoted code comment that you should use ShutdownThread to do a clean shutdown.

    In ShutdownThread, Android does a bunch of things.

    • It shuts down ActivityManager. I think shutting down ActivityManager means that all activities will pass necessary lifecycle and, thus, the states of activities will be stored. But I'm not sure. I did not check.
    • Then, Android turns off the cellular radio interface.
    • After that, it turns off Bluetooth.
    • Finally, it tries to shut MountService down.

    Thus, you can see that it's wrong to do su reboot -p.