Reputation: 44745
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.)
Upvotes: 2
Views: 981
Reputation: 1978
Trigger your c service in initrc:
trigger by property:
on property:sys.shutdown.requested=*
stop cnss-daemon
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.
Upvotes: 1
Reputation: 2113
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"/>
Upvotes: 4