Android 棒棒糖从通知中接收媒体按钮操作?

Android lollipop recieve media button actions from notification?

我正在尝试创建一个使用媒体按钮操作的通知,但我该如何发送和接收这些按钮?这是我目前所拥有的...

if(mediaSession==null) {
            mediaSession = new MediaSession(this, "Boom");
        }
        //Album art
        ImageView img = (ImageView)findViewById(R.id.img_AlbumArt1);
        Bitmap bitmap = ((BitmapDrawable)img.getDrawable()).getBitmap();

        PendingIntent mPauseIntent = PendingIntent.getBroadcast(this, 0,
                    new Intent(ACTION_PAUSE).setPackage(this.getPackageName()),
                    PendingIntent.FLAG_CANCEL_CURRENT);

        Notification.Builder mBuilder =
            new Notification.Builder(this)
                .setVisibility(Notification.VISIBILITY_PUBLIC)
                .setSmallIcon(R.drawable.ic_media_play)
                .setLargeIcon(bitmap)
                .setContentTitle(playingTitle)
                .setContentText(playingArtist)

                .setStyle(new Notification.MediaStyle()
                        .setMediaSession(mediaSession.getSessionToken())
                        .setShowActionsInCompactView(0,1,2)
                )
                // Add media control buttons that invoke intents in your media service
                .addAction(R.drawable.ic_media_rew, "Previous", null) //#0
                .addAction(R.drawable.ic_media_pause, "Play/Pause", null) //#1
                .addAction(R.drawable.ic_media_ff, "Next", null) //#2 nextPendingIntent
            ;
        // Creates an explicit intent for an Activity in your app
        Intent intent = new Intent(this, Music.class);
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_APP_MUSIC);
        // Creates an explicit intent for an Activity in your app
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(this,PendingIntent.FLAG_CANCEL_CURRENT,intent,0);
        mBuilder.setContentIntent(pendingIntent);

        NotificationManager NotiMan=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // mId allows you to update the notification later on.
        NotiMan.notify(notification, mBuilder.build());

Android docs 建议 pendingIntent 我暂时设置了 null,但我一直无法弄清楚如何为此目的制作 PendingIntent .

广播接收器:

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final CharSequence action = intent.getAction();
        if(action==ACTION_PAUSE) {
            //mTransportControls.pause();
            Log.i("ON RECEIVE", "ACTION_PAUSE");
        }
    }
};

并在 onCreate 中:

IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_PAUSE);
this.registerReceiver(mMessageReceiver, filter);
  1. PendingIntent 需要指向一个 BroadcastReceiver 或一个可以处理指定请求的服务。例如,对于 BroadcastReceiver,您将有一个看起来像

    的未决意图
    public static final String ACTION_PAUSE = "com.pkg.name.ACTION_PAUSE";
    
    mPauseIntent = PendingIntent.getBroadcast(mService, REQUEST_CODE,
            new Intent(ACTION_PAUSE).setPackage(pkg),
            PendingIntent.FLAG_CANCEL_CURRENT); 
    
  2. 接下来,注册action/pendingIntent

     .addAction(R.drawable.ic_media_pause, mContext.getString(R.string.label_previous), mPendingIntent);
    
  3. 最后,当 pendingIntent 被触发时,您将处理该操作。这里可以选择使用MediaController.TransportControls到play/pause的动作。

        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            switch (action) {
              case ACTION_PAUSE:
                mTransportControls.pause();
                break;
            ...
        }
    
  4. MediaSession 应该被实例化并设置为作用于 play/pause + 其他动作。

    mSession = new MediaSession(this, "Service");
    mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS |
             MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
    
  5. 最后扩展 MediaSession.MediaSessionCallback 并实现 onPlay()/onPause().. 这反过来将实际用于播放。