我写了一个使用广播接收器捕捉媒体按钮的服务,只有当音乐播放器处于非活动状态时,我才能捕捉媒体按钮事件。如何在音乐播放器处于活动状态时捕获媒体按钮事件…!
MyActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about_and_activation_view);

Log.d("MR", "onCreate - " + getIntent().getAction());

    AudioManager mAudioManager  = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
      ComponentName mediaButtonReceiver = new ComponentName(getPackageName(), HeadsetControlEventReceiver.class.getName());
      mAudioManager.registerMediaButtonEventReceiver(mediaButtonReceiver);
}

我的广播接收器:
public class HeadsetControlEventReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
    try{
        KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);

        if ((event != null) && (event.getKeyCode() == KeyEvent.KEYCODE_HEADSETHOOK)) {

清单XML
  <receiver
   android:name=".HeadsetControlEventReceiver"
   android:enabled="true">
   <intent-filter android:priority="2147483647">
         <action android:name="android.intent.action.MEDIA_BUTTON"/>
        </intent-filter>
  </receiver>

最佳答案

注册后,音乐播放器启动,音乐播放器被优先考虑。现在你需要如何重新注册。一个很酷的轮询机制如何。

08-03 18:59