I am downloading a file using a foreground service, but when I pause the download I want to stop the service, so I did as following
starting the service
val intent = Intent(context, IdmService::class.java)
context.startService(intent)
context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)
stopping the service
context.unbindService(serviceConnection)
context.stopService(Intent(context, IdmService::class.java))
notification update
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())
}
But when I stop the service, it takes 5 seconds to hide the notification. I tried commenting startForeground(STICKY_NOTIFICATION_ID, notification.build())
then it hide the notification instantly. I want to hide the notification instantly when I stop the service. I believe that the startForeground
method is async, that's why after I killed the service is waiting for the startForeground
method to complete.