触发更新后应用销毁后通知被取消
notification being dismissed after app destroyed after updates are triggered
android 文档声明要启动服务,必须实施 onStartCommand
。当我制作 service
并使用 ContextCompat.startForegroundService(...)
启动服务时,它会执行 onStartCommand
方法,此时 即使我退出应用程序 [=26] =] ,服务不会停止(如预期)。
但是,稍后当我通过更改显示的文本来更新通知时,我使用 notificationManager.notify()
方法,现在当我退出应用程序时 ,服务自动停止。我想知道我们是否应该在每次通知更新时强制调用 onStartCommand,不知何故(比如我们应该用 ContextCompat.startForegroundService
更新通知吗?我已经测试了从 api level 19
到 api level 27
和行为都是一样的。如何在更新通知后将服务的 属性 保持为 started
?
I wonder if we are suppose to force call onStartCommand
on every notification update,
不,您不会被迫在每次通知更新时调用 onStartCommand
somehow(like should we update the notification with ContextCompat.startForegroundService
? I've tested this from API level 19 to API level 27 and the behaviour is all the same. How can I maintain the property of the service as started even after I update the notification?
您应该只调用 ContextCompat.startForegroundService
一次以启动 ForegroundService
。
为了在退出应用时更新ForegroundSerice
通知而不被破坏,您可以在每次要更新前台服务通知时使用startForeground()
。
关于此的著名示例将下载服务更新为类似 ProgressBar
.
的下载进度
重要的是:您需要确保在调用 startForeground()
时使用相同的通知 ID
startForeground()
不会调用 onStartCommand
;如果那是可能的,那么我想这将是一个无限循环。
每当你想停止前台服务,并提交一个普通通知(下载示例中的下载完成)时,你可以用notificationManager.notify()
提交一个普通通知,这将允许用户关闭滑动通知。
更新:如聊天中所述
问题:前台服务停止后提交正常通知时;正常通知也被取消。
原因:这是因为您使用了与前台服务相同的通知ID,所以停止前台服务也会关闭正常通知。
解决方法:确保前台服务通知和停止前台服务时派发的普通通知的通知ID不同。
还要确保调用下面的方法来停止前台服务:
stopForeground(true);
stopSelf();
android 文档声明要启动服务,必须实施 onStartCommand
。当我制作 service
并使用 ContextCompat.startForegroundService(...)
启动服务时,它会执行 onStartCommand
方法,此时 即使我退出应用程序 [=26] =] ,服务不会停止(如预期)。
但是,稍后当我通过更改显示的文本来更新通知时,我使用 notificationManager.notify()
方法,现在当我退出应用程序时 ,服务自动停止。我想知道我们是否应该在每次通知更新时强制调用 onStartCommand,不知何故(比如我们应该用 ContextCompat.startForegroundService
更新通知吗?我已经测试了从 api level 19
到 api level 27
和行为都是一样的。如何在更新通知后将服务的 属性 保持为 started
?
I wonder if we are suppose to force call
onStartCommand
on every notification update,
不,您不会被迫在每次通知更新时调用 onStartCommand
somehow(like should we update the notification with
ContextCompat.startForegroundService
? I've tested this from API level 19 to API level 27 and the behaviour is all the same. How can I maintain the property of the service as started even after I update the notification?
您应该只调用 ContextCompat.startForegroundService
一次以启动 ForegroundService
。
为了在退出应用时更新ForegroundSerice
通知而不被破坏,您可以在每次要更新前台服务通知时使用startForeground()
。
关于此的著名示例将下载服务更新为类似 ProgressBar
.
重要的是:您需要确保在调用 startForeground()
startForeground()
不会调用 onStartCommand
;如果那是可能的,那么我想这将是一个无限循环。
每当你想停止前台服务,并提交一个普通通知(下载示例中的下载完成)时,你可以用notificationManager.notify()
提交一个普通通知,这将允许用户关闭滑动通知。
更新:如聊天中所述
问题:前台服务停止后提交正常通知时;正常通知也被取消。
原因:这是因为您使用了与前台服务相同的通知ID,所以停止前台服务也会关闭正常通知。
解决方法:确保前台服务通知和停止前台服务时派发的普通通知的通知ID不同。
还要确保调用下面的方法来停止前台服务:
stopForeground(true);
stopSelf();