Adding a terminate playback button using Android L Notification.MediaStyle

前端 未结 3 1892
南旧
南旧 2021-02-07 06:41

I want to create a Media Playback notification using the new Android L MediaStyle template. Now, I succeeded doing so perfectly well for actions like previous, play, pause, next

相关标签:
3条回答
  • 2021-02-07 07:22

    That's the cancel button you're looking for:

      .setShowCancelButton(true)
      .setCancelButtonIntent(stopPendingIntent)
    

    On your MediaStyle instance.

    0 讨论(0)
  • 2021-02-07 07:34

    Adding on to @matiash's answer:

    1. For Pre-Lollipop his answer is as needed.

      notificationBuilder.setStyle(new NotificationCompat.MediaStyle().setShowCancelButton(true).setCancelButtonIntent(createPlayIntent());
      
    2. For post-Lollipop: To start a media-player notification one must have surely used startForeground() to start the Media Service as a Foreground Service. The problem now is that this Service is not dismissible. EVEN IF we set the setOngoing(false).

    The best practice followed is to make the Service dismissible in the paused state. To do that, when your Media Service receives a Pause state callback, call stopForeground(false). This stops the service, but keeps the notification alive. And it can now be dismissed.

    Happy coding.

    0 讨论(0)
  • 2021-02-07 07:37

    UPDATE

    When using the Support Library's version of this notification style, i.e. NotificationCompat.MediaStyle, there is the setShowCancelButton() button.

    This will add the close button, but only on versions prior to Lollipop, to work around a bug with notifications that cannot be made dismissable after being displayed as part of a foreground service.

    In Lollipop the preferred option is to just being able to dismiss the notification (when the audio is paused) instead of having a custom button to do that.


    OLD ANSWER

    From taking a look at the source code of Notification.MediaStyle class it would seem that there is currently no support for such a button in the MediaStyle notification style:

        private RemoteViews makeMediaBigContentView() {
            final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
            RemoteViews big = mBuilder.applyStandardTemplate(getBigLayoutResource(actionCount),
                    false /* hasProgress */);
    
            if (actionCount > 0) {
                big.removeAllViews(com.android.internal.R.id.media_actions);
                for (int i = 0; i < actionCount; i++) {
                    final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i));
                    big.addView(com.android.internal.R.id.media_actions, button);
                }
            }
            styleText(big);
            hideRightIcon(big);
            applyTopPadding(big);
            big.setViewVisibility(android.R.id.progress, View.GONE);
            return big;
        }
    

    This matches with the layout it inflates (notification_template_material_big_media), which contains:

    • A layout for the media image.
    • A LinearLayout for three text lines.
    • A LinearLayout where the media actions are added.
    • An ImageView to show the divider line between these two.

    but nothing else.

    It would appear that the close button in the documentation page is just an artist's rendition (Google Play Music doesn't include it, either).

    0 讨论(0)
提交回复
热议问题