简体   繁体   中英

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.)

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"/>

Trigger your c service in initrc:

  1. trigger by property:

    on property:sys.shutdown.requested=*
    stop cnss-daemon

  2. trigger by shutdown critical

shutdown <shutdown_behavior> Set shutdown behavior of the service process. When this is not specified, the service is killed during shutdown process by using SIGTERM and SIGKILL. The service with shutdown_behavior of "critical" is not killed during shutdown until shutdown times out. When shutdown times out, even services tagged with "shutdown critical" will be killed. When the service tagged with "shutdown critical" is not running when shut down starts, it will be started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM