媒体样式通知操作按钮不与PendingIntent.getBroadcast()一起使用

时间:2018-04-20 11:16:46

标签: java android notifications

我正在创建一个通知风格为NotificationCompat.MediaStyle()的音乐播放器。

我实现了我的代码,以便在我的通知中点击操作按钮时发送广播。但我的代码似乎不起作用。

这是我的代码

NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
            .setShowWhen(false)
            // Set the Notification style
            .setStyle(new NotificationCompat.MediaStyle()
                      // Attach our MediaSession token
                      .setMediaSession(mediaSession.getSessionToken())
                      // Show our playback controls in the compact notification view.
                      .setShowActionsInCompactView(0, 1, 2))
            // Set the Notification color
            .setColor(getResources().getColor(R.color.colorPrimary))
            // Set the large and small icons
            .setLargeIcon(largeIcon)
            .setSmallIcon(android.R.drawable.stat_sys_headset)
            // Set Notification content information
            .setContentText("Artist Name")
            .setContentTitle("Album name")
            .setContentInfo("Title name")
            // Add playback actions
            .addAction(android.R.drawable.ic_media_previous, "previous", playbackAction(3))
            .addAction(notificationAction, "pause", play_pauseAction)
            .addAction(android.R.drawable.ic_media_next, "next", playbackAction(2));

((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(NOTIFICATION_ID, notificationBuilder.build());

方法playBackAction()如下:

private PendingIntent playbackAction(int actionNumber){

        Intent playbackAction;

        switch(actionNumber){
        case 0:
            // Play
            playbackAction = new Intent(BROADCAST_PLAYBACK);
            playbackAction.setAction(ACTION_PLAY);

            return PendingIntent.getBroadcast(this, actionNumber, playbackAction, 0);
        case 1:
            // Pause
            playbackAction = new Intent(BROADCAST_PLAYBACK);
            playbackAction.setAction(ACTION_PAUSE);

            return PendingIntent.getBroadcast(this, actionNumber, playbackAction, 0);
        case 2:
            // Next track
            playbackAction = new Intent(BROADCAST_PLAYBACK);
            playbackAction.setAction(ACTION_NEXT);

            return PendingIntent.getService(this, actionNumber, playbackAction, 0);
        case 3:
            // Previous track
            playbackAction = new Intent(BROADCAST_PLAYBACK);
            playbackAction.setAction(ACTION_PREVIOUS);

            return PendingIntent.getBroadcast(this, actionNumber, playbackAction, 0);
        default:
            break;
        }
        return null;
    }

这是我的BroadcastReceiver

BroadcastReceiver broadCastPlayback=new BroadcastReceiver(){

        @Override
        public void onReceive(Context context, Intent intent)
        {
            // TODO: Implement this method

            showToast("onReceive");
        }
    };

我在创建服务时注册了广播接收器

IntentFilter filter=new IntentFilter(BROADCAST_PLAYBACK);
registerReceiver(broadCastPlayback,filter);

未调用showToast()内的onReceive方法。我想知道广播是不是发送还是没有收到。

1 个答案:

答案 0 :(得分:1)

您遇到的问题是过滤器缺少在待处理意图中发送的操作。

例如:.addAction(android.R.drawable.ic_media_previous, "previous", playbackAction(3))会发送操作ACTION_PREVIOUS

但是,boradcast接收器仅在BROADCAST_PLAYBACK

上进行过滤

对于您需要添加(或替换BROADCAST_PLAYBACK)缺失操作的过滤器。

以下示例:

filter.addAction(ACTION_PREVIOUS);

以同样的方式添加您已定义的其他操作。