0

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

4

1 回答 1

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

于 2012-10-29T06:07:19.443 回答