Dismissing MediaStyle notifications

只愿长相守 提交于 2020-02-21 05:51:54

问题


New Android MediaStyle notifications in Lollipop don't have a dismiss button. Looks like there is already a bug for it on Google Code.

Does anyone know what's a good workaround for this issue until the bug is resolved?

Should we just delay switching to MediaStyle? Or use one of the actions as the dismiss button?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/26496670/dismissing-mediastyle-notifications

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