有没有什么办法可以收到通知,当用户关闭电源设备? [英] Is there any way to receive a notification when the user powers off the device?

查看:123
本文介绍了有没有什么办法可以收到通知,当用户关闭电源设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

您可以使用<一个href="http://developer.android.com/intl/de/reference/android/content/Intent.html#ACTION_SHUTDOWN"><$c$c>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.

您会套住广播与<一个href="http://developer.android.com/intl/de/reference/android/content/BroadcastReceiver.html"><$c$c>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>

根据你在做什么,另一种选择是使用<一个href="http://developer.android.com/intl/de/reference/android/content/Intent.html#ACTION_BOOT_COMPLETED"><$c$c>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.

这篇关于有没有什么办法可以收到通知,当用户关闭电源设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆