音量变化侦听器: registerMediaButtonEventReceiver 是否比 onKeyDown 更可取? [英] Volume change Listener: Is registerMediaButtonEventReceiver preferable to onKeyDown?

查看:44
本文介绍了音量变化侦听器: registerMediaButtonEventReceiver 是否比 onKeyDown 更可取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找一种最全面且兼容(即所有 Android 版本...)"的方式来收听音量变化,我发现了两种不同的方法来处理这个问题:

Looking for a "most comprehensive & compatible (i.e. all Android versions...)" way to listen to volume changes, I found 2 different approaches to handle this:

  1. registerMediaButtonEventReceiver
  2. onKeyDown + SettingsContentObserver一>
  1. registerMediaButtonEventReceiver
  2. onKeyDown + SettingsContentObserver

哪种方法更可取?

为什么?

更新 1: 感谢下面的评论,我发现 onKeyDown() 实际上接管音量键,作为其中之一,这可能不是一个完整的解决方案帖子提到音量可以通过硬件按钮以外的接口改变(更不用说谷歌似乎正在逐渐取消这些接管"功能).

UPDATE 1: Thanks to the comment below, I discovered that onKeyDown() actually takes over the volume key, which may not be a complete solution as one of the posts mentioned that volume could be changed via interfaces other than the hardware buttons (not to mention that Google seems to be gradually taking away those "take over" capabilities).

OTOH,android.media.VOLUME_CHANGED_ACTION 是一种黑客行为,甚至没有记录.这可能意味着它将在 Android 5 左右停止工作...

OTOH, android.media.VOLUME_CHANGED_ACTION is a hack and isn't even documented. Which probably means it will cease to work in Android 5 or so...

更新 2: registerMediaButtonEventReceiver 根本不起作用!(对于音量硬件按钮,我刚刚试过了).

UPDATE 2: registerMediaButtonEventReceiver doesn't work at all! (for the volume hardware buttons that is, I just tried it).

其他见解?

推荐答案

如果在未来的 API 中为卷流提供一个 BroadcastReceiver 会很棒,但今天也许最好的解决方案是注册一个 BroadcastReceivercode>ContentObserver 用于设置(包括 VOLUME_NOTIFICATION):

It would be great to have in future APIs a BroadcastReceiver for volume streams, but today maybe the best solution is to register a ContentObserver for the settings (that includes VOLUME_NOTIFICATION):

mSettingsContentObserver = new SettingsContentObserver( new Handler() ); 
this.getApplicationContext().getContentResolver().registerContentObserver( 
    android.provider.Settings.System.CONTENT_URI, true,
    mSettingsContentObserver );

有关详细信息,请参阅此答案:https://stackoverflow.com/a/7017516/117382

see this answer for more info: https://stackoverflow.com/a/7017516/117382

用工作代码更正.也许这个解决方案更好:https://stackoverflow.com/a/17398781/117382

Corrected with working code. Maybe this solution is better: https://stackoverflow.com/a/17398781/117382

这篇关于音量变化侦听器: registerMediaButtonEventReceiver 是否比 onKeyDown 更可取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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