【问题标题】:Receive long volume-key press接收长按音量键
【发布时间】:2016-07-13 14:34:10
【问题描述】:

Google-Music-App(在 OnePlus One 上,Cyanogen OS-Version 12.1)在音量增大时播放下一首歌曲硬件键长按 并在音量减小键长按时播放上一首歌曲。

是否可以在 BroadcastReceiver 中接收这些操作? 还是操作系统只是将这些长按作为 MEDIA_BUTTON 动作处理? (如果我的两个假设都不对,Google Play 是如何做到的?)

编辑 1:

似乎这是一个带有“MEDIA_NEXT”的 MEDIA_BUTTON 操作。但是为什么我的接收器没有收到动作呢?

String action = intent.getAction();
    if(action.equalsIgnoreCase(Intent.ACTION_MEDIA_BUTTON)) {
            KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
            if (KeyEvent.KEYCODE_MEDIA_PLAY == event.getKeyCode()) {
                //my play code
            }else if (KeyEvent.KEYCODE_MEDIA_NEXT == event.getKeyCode()) {
                //my play next code
            }else if (KeyEvent.KEYCODE_MEDIA_PREVIOUS == event.getKeyCode()) {
            //my play prev code
        }else if (KeyEvent.KEYCODE_MEDIA_PAUSE == event.getKeyCode()) {
            //my pause code
        }
    }

它在清单中注册:

<receiver android:name=".receiver.AudioPlayerBroadcastReceiver">
        <intent-filter>
            //other actions (they all work fine)
            <action android:name="android.intent.action.MEDIA_BUTTON" />
        </intent-filter>
    </receiver>

【问题讨论】:

    标签: android android-intent media broadcast receiver


    【解决方案1】:

    Cyanogen 在长按音量增大时发送MEDIA_NEXT 事件。 (至少 Cyanogemod 11 在我的手机上做到了这一点)

    您应该能够轻松地对此进行测试。只需收听MEDIA_NEXT 并检查您是否在长按时收到它。

    要编辑 1: 见BroadcastReceiver for ACTION_MEDIA_BUTTON not working

    (另外:如果您有新问题,请打开一个新问题,而不是编辑旧问题)

    【讨论】:

    • 谢谢,刚刚更新了我的问题!你能发现我的问题吗?
    猜你喜欢
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    相关资源
    最近更新 更多