ACTION_MEDIA_BUTTON广播接收器未被解雇

时间:2017-06-12 18:12:19

标签: android android-mediaplayer

我正在开发和应用程序从网上流数据,我使用Android MediaPlayer和AudioManager,一切正常。我被困在ACTION_MEDIA_BUTTON中,通过耳机按钮播放/暂停播放器。

  

仅供参考,我已经使用了stackover中建议的所有方式,setPriority为   999或1000或10000也在android menifeast文件等中注册监听器。   但不是结果。

BroadcarstReceiver文件

public class HeadSetReceiver扩展了BroadcastReceiver {

    onHeadsetEventListener mCallback;


    public interface onHeadsetEventListener {
        void onHeadsetRemoved();

        void onHeadsetNextClicked();

        void onHeadsetPreviousClicked();

        void onHeadsetPlayPauseClicked();
    }


    @Override
    public void onReceive(Context context, Intent intent) {

        mCallback = (onHeadsetEventListener) context;


        // Intent.ACTION_HEADSET_PLUG  << Trigger as soon i plug and unplug the headphone..

        if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
            int state = intent.getIntExtra("state", -1);
            switch (state) {
                case 0:
                    mCallback.onHeadsetRemoved();
                    break;
                case 1:
                    break;
            }
        }

        // Intent.ACTION_MEDIA_BUTTON ---- Not triggered even after register listener
        if (intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON)) {
            KeyEvent event = (KeyEvent) intent
                    .getParcelableExtra(Intent.EXTRA_KEY_EVENT);
            int keycode = event.getKeyCode();
            int action = event.getAction();
            Log.i("keycode", String.valueOf(keycode));
            Log.i("action", String.valueOf(action));
            //onKeyDown(keyCode, event)
            if (keycode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE  || keycode == KeyEvent.KEYCODE_HEADSETHOOK)
                if (action == KeyEvent.ACTION_DOWN)
                    mCallback.onHeadsetPlayPauseClicked();
            if (keycode == KeyEvent.KEYCODE_MEDIA_NEXT)
                if (action == KeyEvent.ACTION_DOWN)
                    mCallback.onHeadsetNextClicked();
            if (keycode == KeyEvent.KEYCODE_MEDIA_PREVIOUS)
                if (action == KeyEvent.ACTION_DOWN)
                    mCallback.onHeadsetPreviousClicked();

        }
    }
}

*我在MediaPlayerService.java中注册了监听器 - OnCreate()as  遵循*

headSetReceiver = new HeadSetReceiver();

IntentFilter filter2 = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
filter2.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
registerReceiver(playerRadioService.headSetReceiver,filter2);

并且还注销了onDestroy,如下所示

unregisterReceiver(headSetReceiver);

但我不确定,我缺少什么来实施,因此没有结果。

  

logcat的

  --------- beginning of system
06-13 00:03:36.316 1192-1202/? D/BroadcastQueue: Add broadcastBroadcastRecord{30a21c3 u0 android.intent.action.MEDIA_BUTTON} into (background/order), now header = BroadcastRecord{30a21c3 u0 android.intent.action.MEDIA_BUTTON}, size = 1
06-13 00:03:36.318 1192-1202/? D/BroadcastQueue: Add broadcastBroadcastRecord{16bfa40 u0 android.intent.action.MEDIA_BUTTON} into (background/order), now header = BroadcastRecord{30a21c3 u0 android.intent.action.MEDIA_BUTTON}, size = 2
06-13 00:03:36.321 1192-1961/? V/BroadcastQueue: Finished with ordered broadcast BroadcastRecord{30a21c3 u0 android.intent.action.MEDIA_BUTTON}, [background], remain = 1
06-13 00:03:36.324 1192-2185/? V/BroadcastQueue: Finished with ordered broadcast BroadcastRecord{16bfa40 u0 android.intent.action.MEDIA_BUTTON}, [background], remain = 0
06-13 00:03:36.755 271-335/? D/phoneserver: rsrp[0]=97,rscp[0]=255,rxlev[0]=99 ind_str= 
                                            +CESQ: 99,99,255,255,22,97

1 个答案:

答案 0 :(得分:0)

您是否在清单文件中声明了接收器?