java - 媒体样式通知操作按钮不适用于 PendingIntent.getBroadcast()

标签 java android notifications android-notifications android-notification.mediastyle

我正在使用 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 broadCastPlayback=new BroadcastReceiver(){

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

            showToast("onReceive");
        }
    };

我在android serviceonCreate中注册了广播接收器

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

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

最佳答案

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

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

但广播接收器仅对 BROADCAST_PLAYBACK 进行过滤

您需要在过滤器中添加(或替换 BROADCAST_PLAYBACK)缺少的操作。

按照示例:

filter.addAction(ACTION_PREVIOUS);

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

关于java - 媒体样式通知操作按钮不适用于 PendingIntent.getBroadcast(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49940441/

相关文章:

java - 通过查找其类来获取对象属性

java - 如何将 Android Wear Activity 类导入移动 Activity 类?

java - tomcat WEB-INF 上的 jar 名称列表

java - 如何正确地将 JSP 放入 WEB-INF 文件夹中?

java - 在 Eclipse 插件中,是否存在与 MarkerAnnotationSpecification 扩展点等效的编程方式?

iOS 警报横幅/标签?

android - 无法在 Android < 5.0 上删除通知

java - 在 JUnit (4.8.1) 测试套件中分配 static final int

android - Google map 首次在我的 Android 应用程序上无法加载

notifications - 如何配置 Mantis 以便向管理员通知系统中的所有问题