Search code examples
androidandroid-sourceshutdownandroid-6.0.1-marshmallow

How to tell when Android is shutting down?


How can I tell when Android is shutting down?

I can put the functionality into a C++ daemon which is already running as root, or into an unprivileged Java app.

Can I distinguish shutting down from rebooting? (I only care about shutting down, and must not get a false-positive when rebooting.)


Solution

  • With a broadcast Receiver:

    @Override public void onReceive(Context context, Intent intent) {
        if (ACTION_SHUTDOWN.equals(intent.getAction()) || QUICKBOOT_POWEROFF.equals(intent.getAction())) {
            // Do something..
        }
    }
    

    Apply an intent filter to your manifest declared broadcast receiver to listen to two actions:

    <action android:name="android.intent.action.ACTION_SHUTDOWN"/>
    <action android:name="android.intent.action.QUICKBOOT_POWEROFF"/>