Android:音乐播放器处于活动状态时捕获媒体按钮操作

时间:2013-01-24 13:41:12

标签: android android-intent

我写了一些服务,它使用BroadcastReceiver来捕获媒体按钮之一。我只有在音乐播放器处于非活动状态时才能捕获媒体按钮事件。当我的音乐播放器处于活动状态时,如何捕获媒体按钮事件..!

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

MyBroadcastReceiver:

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

的Manifest.xml

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

1 个答案:

答案 0 :(得分:-1)

在您注册并启动音乐播放器后,音乐播放器将获得优先权。现在您需要再次注册。如何使用酷轮询机制。