我正在使用前台服务下载文件,但是当我暂停下载时我想停止该服务,所以我按照以下步骤进行操作
启动服务
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
方法完成的原因。
答案 0 :(得分:0)
这似乎与最新的安全补丁有关:
如果具有最新的安全补丁程序,这是预期的行为 已安装-FGS通知必须至少可见5秒钟