问题
I'm looking to stop/dismiss a foreground notification for a service for a mediaplayer, much similar to Google's implementation for Google Music.
For instance in Google Music, if you are playing music then the the notification cannot be swiped away. However if you pause the music it can.
This is completely different to how it is implemented on Android 4.4, where the notification starts only when you leave the app and removes itself when you go back into the app. I can't see how to implement this either considering the requirements for a service to have a notification.
Any help would be much appreciated.
回答1:
How do i remove a foreground notification in Android Lollipop?
You can remove your own foreground Notification
by calling stopForeground()
from your Service
, that called startForeground()
.
For instance in Google Music, if you are playing music then the the notification cannot be swiped away. However if you pause the music, you can swipe it away.
Presumably, they are updating the setOngoing()
value for the Notification
based upon whether or not the music is playing.
回答2:
You can also remove the notification in a tricky way:
- start 1st service with
startForeground(1, new Notification());
- start 2nd service with
startForeground(1, new Notification());
and immediately stop it
As a result 1st service is still running in foreground but the notification is gone thanks to stopping 2nd service. I've found it by accident :).
来源:https://stackoverflow.com/questions/26576872/how-do-i-remove-a-foreground-notification-in-android-lollipop