Browse Source

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

20241218TB223FC(测试jar包)
wangwanlei 9 months ago
parent
commit
03e458cf60

+ 2
- 0
app/src/main/java/com/xhly/manageapp/AppAdapter.kt View File

1
 package com.xhly.manageapp
1
 package com.xhly.manageapp
2
 
2
 
3
+import DownLoadUtils
3
 import android.content.ComponentName
4
 import android.content.ComponentName
4
 import android.content.Context
5
 import android.content.Context
5
 import android.content.Intent
6
 import android.content.Intent
81
                     Toaster.showLong("失败原因"+e.toString())
82
                     Toaster.showLong("失败原因"+e.toString())
82
                 }finally {
83
                 }finally {
83
                     if (destroyFlag){
84
                     if (destroyFlag){
85
+                        DownLoadUtils.destroyDownLoad()
84
                         InstallUtils.destroyInstallUtils()
86
                         InstallUtils.destroyInstallUtils()
85
                     }
87
                     }
86
                 }
88
                 }

+ 12
- 3
app/src/main/java/com/xhly/manageapp/utils/DownLoadUtils.kt View File

5
 import com.xhly.corelib.utils.LogShow
5
 import com.xhly.corelib.utils.LogShow
6
 import kotlinx.coroutines.CoroutineScope
6
 import kotlinx.coroutines.CoroutineScope
7
 import kotlinx.coroutines.Dispatchers
7
 import kotlinx.coroutines.Dispatchers
8
+import kotlinx.coroutines.cancel
8
 import kotlinx.coroutines.launch
9
 import kotlinx.coroutines.launch
9
 import java.util.LinkedList
10
 import java.util.LinkedList
10
 import java.util.Queue
11
 import java.util.Queue
13
     val queue: Queue<DownloadItem> = LinkedList()
14
     val queue: Queue<DownloadItem> = LinkedList()
14
     var callbackList = mutableListOf<DownloadCallback>()
15
     var callbackList = mutableListOf<DownloadCallback>()
15
     var isRunning = false
16
     var isRunning = false
16
-
17
+    var coroutineScope:CoroutineScope? =null
17
     fun isEmpty(): Boolean {
18
     fun isEmpty(): Boolean {
18
         return queue.isEmpty()
19
         return queue.isEmpty()
19
     }
20
     }
59
         if (isRunning) {
60
         if (isRunning) {
60
             return
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
             while (queue.size > 0) {
67
             while (queue.size > 0) {
65
                 isRunning = true
68
                 isRunning = true
66
                 val downItem = queue.peek()
69
                 val downItem = queue.peek()
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
 data class DownloadItem(val url: String, val tag: String, val savepath: String, var progress: Int,var pkgName:String)
135
 data class DownloadItem(val url: String, val tag: String, val savepath: String, var progress: Int,var pkgName:String)

Loading…
Cancel
Save