接收长按音量键

时间:2016-03-25 19:31:10

标签: android android-intent media broadcast receiver

Google-Music-App (在OnePlus One上,Cyanogen操作系统版本12.1)在长时间按下音量增加硬件键时播放下一首歌曲并长按音量调低键播放上一首乐曲。

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

修改1:

好像它是一个MEDIA_BUTTON行动" MEDIA_NEXT"。但为什么我的接收器没有收到动作?

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>

1 个答案:

答案 0 :(得分:1)

Cyanogen在长按音量时发送$_SESSION['arrayId']事件。 (至少Cyanogemod 11在我的手机上做到了这一点)

您应该可以轻松地进行测试。只需听MEDIA_NEXT并查看是否在长按时收到它。

编辑1 : 见BroadcastReceiver for ACTION_MEDIA_BUTTON not working

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

相关问题