如何在Android Lollipop中删除前台通知?
我希望停止/关闭针对媒体播放器的服务的前台通知,这与Google对Google Music的实现非常相似.
I'm looking to stop/dismiss a foreground notification for a service for a mediaplayer, much similar to Google's implementation for Google Music.
例如在Google音乐中,如果您正在播放音乐,则通知不会被清除.但是,如果您暂停音乐,则可以.
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.
这与在Android 4.4上实现的方式完全不同,在Android 4.4中,通知仅在您离开应用程序时才开始,而在您返回应用程序时便会自行删除.考虑到服务具有通知的要求,我既看不到如何实现这一点.
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.
如何在Android Lollipop中删除前台通知?
How do i remove a foreground notification in Android Lollipop?
您可以通过从称为startForeground()
的Service
中调用stopForeground()
来删除自己的前景Notification
.
You can remove your own foreground Notification
by calling stopForeground()
from your Service
, that called startForeground()
.
例如在Google音乐中,如果您正在播放音乐,则通知不会被清除.但是,如果您暂停音乐,则可以将其轻扫一下.
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.
大概是他们根据音乐是否在播放来更新Notification
的setOngoing()
值.
Presumably, they are updating the setOngoing()
value for the Notification
based upon whether or not the music is playing.