Google-Music-App (在 OnePlus One,Cyanogen OS 版本 12.1 上) 在长按音量增大硬件键 并长按音量减小键播放上一首歌曲。
是否可以在 BroadcastReceiver 中接收这些操作? 还是操作系统只是将这些长按作为 MEDIA_BUTTON 操作处理? (如果我的两个假设都不对,Google Play 是如何做到这一点的?)
编辑 1:
似乎是带有“MEDIA_NEXT”的 MEDIA_BUTTON 操作。但是为什么我的接收器没有收到 Action ?
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
}
}
它已在 list 中注册:
<receiver android:name=".receiver.AudioPlayerBroadcastReceiver">
<intent-filter>
//other actions (they all work fine)
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
最佳答案
Cyanogen 在长按音量增大时发送一个 MEDIA_NEXT
事件。 (至少 Cyanogemod 11 在我的手机上做到了这一点)
您应该能够轻松地对此进行测试。只需收听 MEDIA_NEXT
并检查您是否通过长按接收到它。
要编辑 1: 参见 BroadcastReceiver for ACTION_MEDIA_BUTTON not working
(另外:如果您有新问题,请打开一个新问题而不是编辑旧问题)
关于android - 接收长按音量键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36226675/