如何判断 Android 何时关闭?

How to tell when Android is shutting down?

如何判断 Android 何时关闭?

我可以将功能放入已经作为 root 运行 的 C++ 守护程序,或放入非特权 Java 应用程序。

我能区分关机和重启吗? (我只关心关机,重启时一定不要误报。)

使用广播接收器:

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

对清单声明的广播接收器应用 Intent 过滤器以收听两个操作:

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

在 initrc 中触发你的 c 服务:

  1. 由 属性 触发:

    于 property:sys。shutdown.requested=*
    停止 cnss-daemon

  2. shutdown critical

  3. 触发

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.