体积变化监听器:是registerMediaButtonEventReceiver preferable到的onkeydown?监听器、体积、registerMediaButtonEventRecei

2023-09-04 05:23:48 作者:哥,我们可不可以不要这样

寻找一个最COM prehensive和放大器;兼容(即所有的Andr​​oid版本...)一路听音量的变化,我发现了2种不同的方法来处理这​​个问题:

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:

registerMediaButtonEventReceiver 的onkeydown + SettingsContentObserver registerMediaButtonEventReceiver onKeyDown + SettingsContentObserver

哪种方法preferable?

Which method is preferable?

为什么?

更新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是一个黑客和isn't甚至记录。这可能意味着它会停止工作 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 卷流,但今天也许是最好的解决方案是注册一个 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 );

看到这个答案的详细信息: http://stackoverflow.com/a/7017516/117382

编辑:修正与工作code。也许这个解决方案是更好: http://stackoverflow.com/a/17398781/117382

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

 
精彩推荐
图片推荐