问题
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:
For Pre-Lollipop use:
notificationBuilder.setStyle(new NotificationCompat.MediaStyle().setShowCancelButton(true).setCancelButtonIntent(createPlayIntent());
For post-Lollipop: To start a media-player notification one must have surely used
startForeground()
to start the Media Service as aForeground Service
. The problem now is that thisService
is not dismissible. EVEN IF we set thesetOngoing(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