Adding a terminate playback button using Android L Notification.MediaStyle

前端 未结 3 1890
南旧
南旧 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: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.

提交回复
热议问题