删除相关通知后前台服务会发生什么?

时间:2012-10-29 05:57:57

标签: android service

我有一个在前台运行的服务。我正在寻找一种方法来阻止服务在用户触摸通知时在前台运行。我强调通过调用stopforeground函数可以删除前台服务。我提到了关于前台服务的android dev文档,但是当与它关联的通知被删除时,它没有提到前台服务的行为。所以我的问题是 1)可以通过创建在用户触摸时清除的通知来停止前台服务吗? 2)如何确定服务不再在前台运行?

1 个答案:

答案 0 :(得分:0)

来自文档:

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)之类的调用,以便停止服务并删除通知。

相关问题