如何从耳机按钮接收意图

时间:2017-05-08 06:48:03

标签: android android-intent

简单的问题是如何通过耳机按钮接收意图?我已经搜索了一段时间,没有任何结果。 我的应用程序针对API 15-25

  中的

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

在活动中的onCreate

    IntentFilter mediaIntent = new IntentFilter();
    mediaIntent.addAction(Intent.ACTION_MEDIA_BUTTON);
    mediaIntent.setPriority(999999999);



    registerReceiver(mMemoryStorage.mBroadcastReceiver, mediaIntent);
  

和BroadcastReceiver中的onReceive

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

            }

0 个答案:

没有答案