android - 通知中不显示按钮

标签 android android-notifications custom-notification

我正在服务 (MusicService) 中创建自定义通知并添加按钮,如下所示。

//Notification
    Intent notIntent = new Intent(this, MainActivity.class);
    notIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendInt = PendingIntent.getActivity(this, 0,
            notIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    //Play
    Intent playIntent = new Intent(this, MusicService.class);
    playIntent.setAction(ACTION_PLAY);
    PendingIntent pPlayIntent = PendingIntent.getService(this, 1,
            playIntent, 0);

    //Pause
    Intent pauseIntent = new Intent(this, MusicService.class);
    pauseIntent.setAction(ACTION_PAUSE);
    PendingIntent pPauseIntent = PendingIntent.getService(this, 1,
            playIntent, 0);

    //Previous
    Intent previousSongIntent = new Intent(this, MusicService.class);
    previousSongIntent.setAction(ACTION_PREVIOUS);
    PendingIntent pPreviousSongIntent = PendingIntent.getService(this, 1,
            previousSongIntent, 0);

    //Next
    Intent nextSongIntent = new Intent(this, MusicService.class);
    nextSongIntent.setAction(ACTION_NEXT);
    PendingIntent pNextSongIntent = PendingIntent.getService(this, 1,
            nextSongIntent,0);

    Notification.Builder builder = new Notification.Builder(this);
    builder.setSmallIcon(R.mipmap.ic_launcher)
            .setOngoing(true)
            .setContentText(songTitle)
            .setContentIntent(pendInt)
            .setPriority(Notification.PRIORITY_MAX)
            .setWhen(0)
            .addAction(R.drawable.ic_play_button, "PLAY", pPlayIntent)
            .addAction(R.drawable.ic_pause_button, "PAUSE", pPauseIntent)
            .addAction(R.drawable.ic_next_button, "NEXT", pNextSongIntent)
            .addAction(R.drawable.ic_previous_button, "PREV", pPreviousSongIntent);
      Notification notification = builder.build();
      startForeground(NOTIFY_ID, notification);

通知中不显示按钮图标。 使用两根手指向下滑动通知显示文本(播放暂停下一个)。 我错过了什么吗? enter image description here

最佳答案

MediaStylesetShowActionsInCompactView 结合使用

builder.setStyle(androidx.media.app.NotificationCompat.MediaStyle()
                    .setShowActionsInCompactView(0, 1, 2))

完整示例在这里 https://code.videolan.org/videolan/vlc-android/-/blob/master/application/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.kt#L99

关于android - 通知中不显示按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49181049/

相关文章:

android - 以编程方式设置按钮背景颜色删除边距和圆角

java - 根据应用程序是在前台还是后台,通知显示不同

android - PendingIntent 不适用于 Android O

java - 如何从 URL 图像 Nougat 及以上操作系统版本设置自定义通知图标?

android - fragment isVisible() 为真时不可见

android - AlphaAnimation 对象不会更改动画 View 的 alpha 属性,为什么?

android - 使用 android studio new APK 更新 Google play existing(eclipse) APK

java - 状态栏和主通知的单独通知图标

android - 如何让通知始终固定在通知栏顶部