我有一个服务在前台运行。我正在寻找一种方法,以停止服务运行在前台时,用户触摸通知。我明白了前台服务可以通过调用stopforeground函数来移除。我参考了android dev文档中关于前台服务的内容,但是它并没有提到前台服务在相关通知被移除时的行为。所以我的问题是1)可以通过创建一个通知来停止前台服务,当用户触摸它时清除它?2)如何确定服务不再在前台运行?
发布于 2012-10-29 14:07:19
从文档中:
public final void stopForeground (boolean removeNotification)
Added in API level 5
Remove this service from foreground state, allowing it to be killed if more memory is needed.
Parameters
removeNotification If true, the notification previously provided to startForeground(int, Notification) will be removed. Otherwise it will remain until a later call removes it (or the service is destroyed).基本上,它说如果一个服务被销毁,那么它的通知也将自动删除。但是,如果一个服务被简单地停止,并且提供的参数是false,那么通知将一直保留,直到后面的调用将其删除或服务被销毁。
因此,在通知的回调中,您可以包括像stopForeground(true)这样的调用,这样服务就会停止,通知也会被删除。
https://stackoverflow.com/questions/13116576
复制相似问题