【发布时间】: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