当发送带有 ACTION_MEDIA_BUTTON 意图的 sendOrderedBroadcast 时(我在模仿用户正在单击蓝牙耳机上的播放按钮),Google Play 音乐打开并播放最后播放的专辑,而不是前台音乐播放应用程序。
如果我将其更改为 sendBroadcast,Google Play 音乐和当前的音乐播放应用程序(在我的例子中是 Pandora)都会启动播放按钮。
这仅发生在 Android 4.0 及更高版本中。Play Music 是否有这种意图(一个错误)?您是否怀疑 Pandora 没有按照以下建议将自己注册为当前的媒体按钮处理程序: http ://android-developers.blogspot.com/2010/06/allowing-applications-to-play-nicer.html
有没有办法可以将这种意图仅指向当前的音乐播放应用程序?
这是我的代码:
public void broadcastMediaIntent(MediaIntent intentType){
long eventtime = SystemClock.uptimeMillis();
Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent downEvent = null;
KeyEvent upEvent = null;
switch(intentType){
case NEXT:
downEvent = new KeyEvent(eventtime, eventtime,
KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT, 0);
upEvent = new KeyEvent(eventtime, eventtime,
KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT, 0);
break;
case PLAY_PAUSE:
downEvent = new KeyEvent(eventtime, eventtime,
KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0);
upEvent = new KeyEvent(eventtime, eventtime,
KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0);
break;
case PREVIOUS:
downEvent = new KeyEvent(eventtime, eventtime,
KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS, 0);
upEvent = new KeyEvent(eventtime, eventtime,
KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PREVIOUS, 0);
break;
case FAST_FORWARD:
downEvent = new KeyEvent(eventtime, eventtime,
KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_FAST_FORWARD, 0);
upEvent = new KeyEvent(eventtime, eventtime,
KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_FAST_FORWARD, 0);
break;
case REWIND:
downEvent = new KeyEvent(eventtime, eventtime,
KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_REWIND, 0);
upEvent = new KeyEvent(eventtime, eventtime,
KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_REWIND, 0);
break;
default:
break;
}
downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
sendOrderedBroadcast(downIntent, null);
upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent);
sendOrderedBroadcast(upIntent, null);
}