有没有什么办法可以收到通知,当用户关闭电源设备?有没有什么、电源、办法、通知

由网友(兮颜)分享简介:我需要知道当用户关机他/她的电话。是否有任何广播(或类似),当用户的手机处于关机状态?该通知I need to know when the user powers off his/her phone. Are there any broadcasts (or similar) that notify when th...

我需要知道当用户关机他/她的电话。是否有任何广播(或类似),当用户的手机处于关机状态?该通知

I need to know when the user powers off his/her phone. Are there any broadcasts (or similar) that notify when the user's phone is powered off?

推荐答案

您可以使用ACTION_SHUTDOWN意图当手机即将关闭其播放。该文件说:

You can use the ACTION_SHUTDOWN Intent which is broadcast when the phone is about to shutdown. The documentation says:

应用程序将无法正常需要处理这个问题,因为前台活动将被暂停为好。

Apps will not normally need to handle this, since the foreground activity will be paused as well.

在换句话说,如果你所有的生命周期事件的活动做出适当的反应,没有必要使用这个,除非你真的想要做一些具体的事情涉及到关机。

In other words, if you respond to all the lifecycle events for your Activity appropriately, there's no need to use this unless you really want to do something specific related to shutdown.

ACTION_SHUTDOWN 目的是引入API级别4,换句话说这将只在手机上发送运行Android 1.6或更高版本。

The ACTION_SHUTDOWN Intent was introduced in API Level 4, in other words it'll only be sent on phones running Android 1.6 or later.

您会套住广播与BroadcastReceiver.它看起来是这样的:

You'll trap the Broadcast with a BroadcastReceiver. It will look something like this:

public class ShutdownReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        //Insert code here
    }

}

您还需要在你的清单如下所示的条目:

You'll also need an entry in your Manifest like the following:

<receiver android:name=".ShutdownReceiver">
  <intent-filter>
    <action android:name="android.intent.action.ACTION_SHUTDOWN" />
  </intent-filter>
</receiver>

根据你在做什么,另一种选择是使用ACTION_BOOT_COMPLETED当手机重新启动被发送意图。

Depending on what you're doing, another option would be to use the ACTION_BOOT_COMPLETED Intent which is sent when the phone is restarted.

阅读全文

相关推荐

最新文章