更新前台服务通知会延迟停止服务

时间:2020-01-31 17:43:31

标签: java android kotlin foreground-service

我正在使用前台服务下载文件,但是当我暂停下载时我想停止该服务,所以我按照以下步骤进行操作

启动服务

val intent = Intent(context, IdmService::class.java)
context.startService(intent)
context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)

停止服务

context.unbindService(serviceConnection)
context.stopService(Intent(context, IdmService::class.java))

通知更新

private fun updateInfo(title: String, downloadedSize: Long, totalSize: Long) {
    notification.setContentTitle(title)
    notification.setContentText("${formatBytes(downloadedSize)}/${formatBytes(totalSize)}")
    notification.setContentInfo(downloadSpeed)
    notification.setProgress(100, progress, false)
    startForeground(STICKY_NOTIFICATION_ID, notification.build())
}

但是当我停止服务时,需要5秒钟才能隐藏通知。我尝试评论startForeground(STICKY_NOTIFICATION_ID, notification.build()),然后立即隐藏了通知。我想在停止服务时立即隐藏通知。我相信startForeground方法是异步的,这就是为什么我杀死服务后等待startForeground方法完成的原因。

1 个答案:

答案 0 :(得分:0)

这似乎与最新的安全补丁有关:

如果具有最新的安全补丁程序,这是预期的行为 已安装-FGS通知必须至少可见5秒钟

https://issuetracker.google.com/issues/147792378

相关问题