@@ -24,6 +24,7 @@ import java.io.IOException
2424import kotlin.math.roundToInt
2525import kotlinx.coroutines.CoroutineScope
2626import kotlinx.coroutines.Dispatchers
27+ import kotlinx.coroutines.SupervisorJob
2728import kotlinx.coroutines.launch
2829import okhttp3.ResponseBody
2930import org.ole.planet.myplanet.MainApplication.Companion.createLog
@@ -50,6 +51,9 @@ class MyDownloadService : Service() {
5051 private var totalDownloadsCount = 0
5152 private var completedDownloadsCount = 0
5253
54+ private val downloadJob = SupervisorJob ()
55+ private val downloadScope = CoroutineScope (downloadJob + Dispatchers .IO )
56+
5357 override fun onBind (intent : Intent ? ): IBinder ? = null
5458
5559 override fun onStartCommand (intent : Intent ? , flags : Int , startId : Int ): Int {
@@ -75,7 +79,7 @@ class MyDownloadService : Service() {
7579
7680 updateNotificationForBatchDownload()
7781
78- CoroutineScope ( Dispatchers . IO ) .launch {
82+ downloadScope .launch {
7983 urls.forEachIndexed { index, url ->
8084 currentIndex = index
8185 initDownload(url, fromSync)
@@ -343,6 +347,7 @@ class MyDownloadService : Service() {
343347 stopForeground(true )
344348 } catch (_: Exception ) {
345349 }
350+ downloadJob.cancel()
346351 notificationManager?.cancel(ONGOING_NOTIFICATION_ID )
347352 super .onDestroy()
348353 }
0 commit comments