在android中处理手机关机事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10448304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
handling phone shutdown event in android
提问by Ton
Could you post a simple code to handle shutdown event. I need manifest and receiver.
您能否发布一个简单的代码来处理关闭事件。我需要清单和接收器。
I need the following: I have a service running in background and I want to detect phone shutting down in order to make it in silent mode so that the terrible operator music doesn't play.
我需要以下内容:我有一项在后台运行的服务,我想检测手机关机情况以使其处于静音模式,以便不播放糟糕的操作员音乐。
After restarting the phone (I already have on boot receiver) the service will enable phone sound.
重新启动手机(我已经在启动接收器上)后,该服务将启用手机声音。
回答by waqaslam
You need to listen for the following intent action in your receiver:
您需要在接收器中监听以下意图操作:
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
To make it compatible with some HTC (or other) devices which offer quick power-off feature, you need to include the following action too with in the same intent-filter:
为了使其与某些提供快速关机功能的 HTC(或其他)设备兼容,您还需要在同一个意图过滤器中包含以下操作:
<action android:name="android.intent.action.QUICKBOOT_POWEROFF" />
For more info, read this
有关更多信息,请阅读此
回答by vishesh chandra
<receiver android:name=".ShutdownReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
<action android:name="android.intent.action.QUICKBOOT_POWEROFF" />
</intent-filter>
</receiver>
回答by Som
public static final String ACTION_SHUTDOWN
公共静态最终字符串 ACTION_SHUTDOWN
Broadcast Action: Device is shutting down. This is broadcast when the device is being shut down (completely turned off, not sleeping). Once the broadcast is complete, the final shutdown will proceed and all unsaved data lost. Apps will not normally need to handle this, since the foreground activity will be paused as well.
广播操作:设备正在关闭。这是在设备关闭(完全关闭,不休眠)时广播的。广播完成后,将进行最终关机,所有未保存的数据都会丢失。应用程序通常不需要处理此问题,因为前台活动也会暂停。
This is a protected intent that can only be sent by the system.
这是一个受保护的意图,只能由系统发送。
Constant Value: "android.intent.action.ACTION_SHUTDOWN
"
常数值:“ android.intent.action.ACTION_SHUTDOWN
”
And this ACTION_SHUTDOWN may differ from device to device
这个 ACTION_SHUTDOWN 可能因设备而异
Like HTC its android.intent.action.QUICKBOOT_POWEROFF
像HTC一样 android.intent.action.QUICKBOOT_POWEROFF