Dismissing MediaStyle notifications

試著忘記壹切 提交于 2019-12-04 17:13:21

One mechanism which appears to work quite well is make the notification ongoing while music is playing and make it not ongoing (allowing it to be swipe dismissed) when the music is paused. This seems to be the technique that Google Music already uses.

Adding on to @ianhanniballake's answer:

  1. For Pre-Lollipop use:

    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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!