Android 如何从耳机按钮接收意图

Android 如何从耳机按钮接收意图,android,android-intent,Android,Android Intent,简单的问题是如何从耳机按钮接收意图?我找了一段时间,什么也没找到。 我的应用程序以API 15-25为目标 明显地 和在广播接收器中的onReceive 可能重复:我最近试过,但运气不好,我会再试一次并告诉你结果。我已经彻底再试了一次,效果很好,非常感谢。很高兴听到。。继续编程……) <activity android:name=".Home" android:windowSoftInputMode="stateAlwaysHidden"

简单的问题是如何从耳机按钮接收意图?我找了一段时间,什么也没找到。 我的应用程序以API 15-25为目标

明显地

和在广播接收器中的onReceive


可能重复:我最近试过,但运气不好,我会再试一次并告诉你结果。我已经彻底再试了一次,效果很好,非常感谢。很高兴听到。。继续编程……)
        <activity
        android:name=".Home"
        android:windowSoftInputMode="stateAlwaysHidden"
        android:screenOrientation="portrait"
        android:configChanges="keyboardHidden|orientation">


        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />

            <action 
            android:name="com.example.action.PAUSE_PLAY_INTENT_FILTER" />
            //this should be unique string as action
            <action 
            android:name="com.example.action.NEXT_TRACK_INTENT_FILTER" />
            //this should be unique string as action
            <action 
            android:name="com.example.action.PREVIOUS_TRACK_INTENT_FILTER" 
            />
            //this should be unique string as action
            <action android:name="android.media.VOLUME_CHANGED_ACTION" />
            <action 
            android:name="com.example.action.UPDATE_ALL_LISTS_IN_APP" />
            //this should be unique string as action/>
            <action android:name="android.intent.action.MEDIA_BUTTON" />
            </intent-filter>




            </activity>
    IntentFilter mediaIntent = new IntentFilter();
    mediaIntent.addAction(Intent.ACTION_MEDIA_BUTTON);
    mediaIntent.setPriority(999999999);



    registerReceiver(mMemoryStorage.mBroadcastReceiver, mediaIntent);
String intentAction = intent.getAction();
        if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)){
            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&&action == KeyEvent.ACTION_DOWN) {

                if(mMemoryStorage!=null){
                    System.out.println("playpause");
                    if(mMemoryStorage.musicSrv.isPng()){

                        mMemoryStorage.musicSrv.pausePlayer();
                        mMemoryStorage.musicSrv.updateNotification();
                    } else if(mMemoryStorage.musicSrv.isPaused){
                        mMemoryStorage.musicSrv.playPlayer();
                        mMemoryStorage.musicSrv.updateNotification();
                    }



                    //update seekbar playback in PlaySongFragment

                    Message message = new Message();
                    message.what = Params.UPDATE_SEEKBAR_PLAYSONGFRAGMENT;

                    mMemoryStorage.mHandler.sendMessage(message);
                }

            }