How do i remove a foreground notification in Android Lollipop?

故事扮演 提交于 2019-12-05 05:31:06
CommonsWare

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.

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 :).

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