本文介绍了Android版的emulate /发送ACTION_MEDIA_BUTTON意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我想通过模拟耳机的音乐控制来控制通用的音乐播放(播放/暂停,跳过等)。有没有办法播出ACTION_MEDIA_BUTTON意图是什么?我试着用sendBroadcast()的常规方法,但没有奏效。

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.

这可能吗?

推荐答案

试试这个

  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);

这篇关于Android版的emulate /发送ACTION_MEDIA_BUTTON意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 14:48