소스 검색

1.打开应用商店则取消下载任务

20241218TB223FC(测试jar包)
wangwanlei 9 달 전
부모
커밋
03e458cf60
2개의 변경된 파일14개의 추가작업 그리고 3개의 파일을 삭제
  1. 2
    0
      app/src/main/java/com/xhly/manageapp/AppAdapter.kt
  2. 12
    3
      app/src/main/java/com/xhly/manageapp/utils/DownLoadUtils.kt

+ 2
- 0
app/src/main/java/com/xhly/manageapp/AppAdapter.kt 파일 보기

@@ -1,5 +1,6 @@
1 1
 package com.xhly.manageapp
2 2
 
3
+import DownLoadUtils
3 4
 import android.content.ComponentName
4 5
 import android.content.Context
5 6
 import android.content.Intent
@@ -81,6 +82,7 @@ class AppAdapter(var context: Context, var data: ArrayList<AppInfo>) :
81 82
                     Toaster.showLong("失败原因"+e.toString())
82 83
                 }finally {
83 84
                     if (destroyFlag){
85
+                        DownLoadUtils.destroyDownLoad()
84 86
                         InstallUtils.destroyInstallUtils()
85 87
                     }
86 88
                 }

+ 12
- 3
app/src/main/java/com/xhly/manageapp/utils/DownLoadUtils.kt 파일 보기

@@ -5,6 +5,7 @@ import com.xhly.corelib.Const
5 5
 import com.xhly.corelib.utils.LogShow
6 6
 import kotlinx.coroutines.CoroutineScope
7 7
 import kotlinx.coroutines.Dispatchers
8
+import kotlinx.coroutines.cancel
8 9
 import kotlinx.coroutines.launch
9 10
 import java.util.LinkedList
10 11
 import java.util.Queue
@@ -13,7 +14,7 @@ object DownLoadUtils {
13 14
     val queue: Queue<DownloadItem> = LinkedList()
14 15
     var callbackList = mutableListOf<DownloadCallback>()
15 16
     var isRunning = false
16
-
17
+    var coroutineScope:CoroutineScope? =null
17 18
     fun isEmpty(): Boolean {
18 19
         return queue.isEmpty()
19 20
     }
@@ -59,8 +60,10 @@ object DownLoadUtils {
59 60
         if (isRunning) {
60 61
             return
61 62
         }
62
-        val coroutineScope = CoroutineScope(Dispatchers.IO)
63
-        coroutineScope.launch {
63
+        if (coroutineScope==null){
64
+            coroutineScope = CoroutineScope(Dispatchers.IO)
65
+        }
66
+        coroutineScope?.launch {
64 67
             while (queue.size > 0) {
65 68
                 isRunning = true
66 69
                 val downItem = queue.peek()
@@ -121,6 +124,12 @@ object DownLoadUtils {
121 124
         }
122 125
 
123 126
     }
127
+
128
+    fun destroyDownLoad(){
129
+        coroutineScope?.cancel()
130
+        isRunning=false
131
+        queue.clear()
132
+    }
124 133
 }
125 134
 
126 135
 data class DownloadItem(val url: String, val tag: String, val savepath: String, var progress: Int,var pkgName:String)

Loading…
취소
저장