简体   繁体   English

如何判断Android何时关闭?

[英]How to tell when Android is shutting down?

How can I tell when Android is shutting down? 如何得知Android何时关闭?

I can put the functionality into a C++ daemon which is already running as root, or into an unprivileged Java app. 我可以将功能放入已经以root用户身份运行的C ++守护程序中,或放入未特权的Java应用程序中。

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: 在initrc中触发您的c服务:

  1. trigger by property: 按属性触发:

    on property:sys.shutdown.requested=* 在属性上:sys.shutdown.requested = *
    stop cnss-daemon 停止cnss-daemon

  2. trigger by shutdown critical 紧急关机触发

shutdown <shutdown_behavior> Set shutdown behavior of the service process. shutdown <shutdown_behavior>设置服务进程的关闭行为。 When this is not specified, the service is killed during shutdown process by using SIGTERM and SIGKILL. 如果未指定此选项,则在关闭过程中通过使用SIGTERM和SIGKILL终止服务。 The service with shutdown_behavior of "critical" is not killed during shutdown until shutdown times out. shutdown_behavior为“ critical”的服务在关闭期间不会被杀死,直到关闭超时为止。 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. 当关闭启动时,标有“关闭严重”的服务未运行时,它将启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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