简体   繁体   English

屏幕关闭时释放WAKELOCK

[英]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: 简而言之,您可以通过两种方式实现这一目标:

  1. Check if the HDMI gets disconnected (mainly works on phones, keep reading for TV) 检查HDMI是否断开连接(主要在手机上使用,请继续看电视)
  2. Check if the audio channel becomes noisy. 检查音频通道是否嘈杂。 As per Android documentation ( https://developer.android.com/guide/topics/media/mediaplayer.html#noisyintent ) you can do something like the following (change with ): 根据Android文档( https://developer.android.com/guide/topics/media/mediaplayer.html#noisyintent ),您可以执行以下操作(更改为):

"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.

相关问题 屏幕关闭时,Android Wakelock无法正常工作 - Android Wakelock not working when screen is off WakeLock.release()会关闭屏幕,即使用户正在与应用程序进行交互 - WakeLock.release() switches off screen even if the user was interacting with the app 使用WakeLock时,屏幕打开然后关闭的速度过快(几秒钟没有打开) - Screen on and then off too fast when using WakeLock (not remaining on for a few second) WakeLock没有释放且屏幕没有关闭 - WakeLock not releasing and Screen isn't turning off 即使使用WakeLock,当Droid / Nexus One屏幕关闭时,Accelerometer也会停止提供样品 - Accelerometer stops delivering samples when the screen is off on Droid/Nexus One even with a WakeLock 使用部分唤醒锁关闭屏幕时阻止应用程序进入OnPause - Prevent app from going on OnPause when turning screen off using partial wakelock 使用“ PROXIMITY_SCREEN_OFF_WAKE_LOCK”唤醒锁后,不应该触发的触摸事件UP - Touch event UP being fired when not supposed to, after using “PROXIMITY_SCREEN_OFF_WAKE_LOCK” wakelock 屏幕会使用PowerManager自动唤醒。ON_AFTER_RELEASEakeLock - Screen automatically wakes up with PowerManager.ON_AFTER_RELEASE wakeLock WakeLock仅在充电时打开屏幕 - WakeLock turns screen on only when charging 广播接收器是否需要ACTION_SCREEN_OFF唤醒锁 - Is wakelock needed in broadcast receiver for ACTION_SCREEN_OFF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM