Android emulate/send ACTION_MEDIA_BUTTON intent

强颜欢笑 提交于 2020-01-01 12:00:16

问题


I am trying to control universal music playback (play/pause, skip etc.) by emulating headset music controls. Is there a way to broadcast the ACTION_MEDIA_BUTTON intent? I tried the regular way with sendBroadcast() but it didn't work.

Is this possible?


回答1:


Try this

  long eventtime = SystemClock.uptimeMillis(); 

  Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); 
  KeyEvent downEvent = new KeyEvent(eventtime, eventtime, 
KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0); 
  downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent); 
  sendOrderedBroadcast(downIntent, null); 

  Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); 
  KeyEvent upEvent = new KeyEvent(eventtime, eventtime, 
KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0); 
  upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent); 
  sendOrderedBroadcast(upIntent, null); 


来源:https://stackoverflow.com/questions/12925896/android-emulate-send-action-media-button-intent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!