AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
am.dispatchMediaKeyEvent(downEvent);
upEvent = new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
am.dispatchMediaKeyEvent(upEvent);
3 回答
你试过这个 -
编辑:
备用解决方案,使用PackageManager,Intent,ResolveInfo等以编程方式列出所有音乐应用程序,然后将命令发送到列表中的所有应用程序 .
从API级别19开始,引入了新的API
dispatchMediaKeyEvent()
.此媒体事件将仅发送给当前正在运行的音乐播放器,而不是发送给所有播放器 .
谢谢
广播意图不能正常工作以及支持Spotify . 通过解决方案查看我的answer .