[英]Release WAKELOCK when screen is off
I'm making an Android TV and Amazon Fire TV app that uses WAKELOCK to prevent the TV device from going to sleep. 我正在制作一个使用WAKELOCK防止电视设备进入睡眠状态的Android TV和Amazon Fire TV应用程序。 What I need to do though is release the WAKELOCK when the screen gets turned off, eg when someone presses the power button on the TV, as in this case the Amazon Fire TV Stick etc stay active although the TV is powered off. 我要做的是在屏幕关闭时(例如,当有人按下电视上的电源按钮时)松开WAKELOCK,因为在这种情况下,即使电视已关闭,Amazon Fire TV Stick等仍保持活动状态。
I then need to re-add the WAKELOCK when the TV is powered on. 然后,我需要在电视开机时重新添加WAKELOCK。 What is the accepted best practice for handling this? 处理此问题的公认最佳实践是什么?
EDIT: as per comment I'm updating this response with the most effective method. 编辑:根据评论,我正在用最有效的方法更新此响应。
In a nutshell you can achieve this in two ways: 简而言之,您可以通过两种方式实现这一目标:
"You can ensure your app stops playing music in these situations by handling the ACTION_AUDIO_BECOMING_NOISY intent, for which you can register a receiver by adding the following to your manifest: “您可以通过处理ACTION_AUDIO_BECOMING_NOISY意图来确保您的应用在这些情况下停止播放音乐,为此您可以通过在清单中添加以下内容来注册接收者:
<receiver android:name=".MusicIntentReceiver">
<intent-filter>
<action android:name="android.media.AUDIO_BECOMING_NOISY" />
</intent-filter>
</receiver>
This registers the MusicIntentReceiver class as a broadcast receiver for that intent. 这会将MusicIntentReceiver类注册为该意图的广播接收器。 You should then implement this class: 然后,您应该实现此类:
public class MusicIntentReceiver extends android.content.BroadcastReceiver {
@Override
public void onReceive(Context ctx, Intent intent) {
if (intent.getAction().equals(
android.media.AudioManager.ACTION_AUDIO_BECOMING_NOISY)) {
// signal your service to stop playback
// (via an Intent, for instance)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.