Browse Source

1.添加App下载框架

master
wangwanlei 9 months ago
parent
commit
ff4500b509

+ 34
- 2
app/src/main/java/com/xhly/manageapp/service/websocket/AppSocketService.kt View File

@@ -3,11 +3,15 @@ package com.xhly.manageapp.service.websocket
3 3
 import android.app.Notification
4 4
 import android.app.NotificationChannel
5 5
 import android.app.NotificationManager
6
+import android.app.Service
6 7
 import android.content.ComponentName
7 8
 import android.content.Context
8 9
 import android.content.Intent
9 10
 import android.os.Build
11
+import android.os.Environment
10 12
 import android.widget.Toast
13
+import com.billbook.lib.downloader.Download
14
+import com.billbook.lib.downloader.Downloader
11 15
 import com.xhly.corelib.Const
12 16
 import com.xhly.corelib.bean.AppInfo
13 17
 import com.xhly.corelib.eventbus.UIEvent
@@ -27,12 +31,15 @@ import com.xhly.websocket.service.LongConnService
27 31
 import com.xhly.websocket.service.WebSocketHelper
28 32
 import com.xhly.websocket.utils.SocketPushUtils.dealPushMessage
29 33
 import kotlinx.coroutines.CoroutineScope
34
+import kotlinx.coroutines.Dispatchers
30 35
 import kotlinx.coroutines.MainScope
31 36
 import kotlinx.coroutines.cancel
32 37
 import kotlinx.coroutines.launch
38
+import kotlinx.coroutines.withContext
33 39
 import org.greenrobot.eventbus.EventBus
34 40
 import org.greenrobot.eventbus.Subscribe
35 41
 import org.greenrobot.eventbus.ThreadMode
42
+import java.io.File
36 43
 import java.util.UUID
37 44
 
38 45
 open class AppSocketService : LongConnService() {
@@ -65,9 +72,9 @@ open class AppSocketService : LongConnService() {
65 72
         /* startNotification()*/
66 73
         LogShow("已经创建")
67 74
         appList = AppUtils.GetAppList(this) as ArrayList<AppInfo>
75
+        //downLoadFile("")
68 76
     }
69 77
 
70
-
71 78
     override fun processTextMessage(text: String) {
72 79
         //Log.v("websocket收到的消息", text);
73 80
         dealPushMessage(text)
@@ -207,7 +214,7 @@ open class AppSocketService : LongConnService() {
207 214
         intent.setPackage("com.xhly.manageapp")
208 215
         intent.setAction("test")
209 216
         sendBroadcast(intent)
210
-        LogShow("已经销毁")
217
+        LogShow("Service被销毁")
211 218
     }
212 219
 
213 220
     private fun startNotification() {
@@ -223,4 +230,29 @@ open class AppSocketService : LongConnService() {
223 230
             startForeground(1, notification)
224 231
         }
225 232
     }
233
+
234
+    private fun downLoadFile(url: String) {
235
+        serviceScope.launch {
236
+            withContext(Dispatchers.IO) {
237
+                //val file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
238
+                val file = getExternalFilesDir(null)
239
+                val name = file.toString()
240
+                val downloader = Downloader.Builder().build()
241
+                val request = Download.Request.Builder()
242
+                    .url("https://dl.hdslb.com/mobile/latest/android64/iBiliPlayer-bili.apk?t=20230728&spm_id_from=333.47.b_646f776e6c6f61642d6c696e6b.1")
243
+                    .into(File(name, "test.apk"))
244
+                    .build()
245
+                LogShow("开始下载")
246
+                val execute = downloader.newCall(request).execute()
247
+                val successful = execute.isSuccessful()
248
+                LogShow("下载结果" + successful)
249
+                if (successful) {
250
+                    withContext(Dispatchers.Main){
251
+                        ManageApplication.installPackage(File(name, "test.apk").toString())
252
+                    }
253
+                }
254
+                LogShow("下载后,开始安装")
255
+            }
256
+        }
257
+    }
226 258
 }

+ 3
- 0
corelib/build.gradle.kts View File

@@ -94,4 +94,7 @@ dependencies {
94 94
 
95 95
     //圆角imageview
96 96
     api("com.makeramen:roundedimageview:2.3.0")
97
+
98
+    //下载框架
99
+    api("io.github.ydxlt:okdownloader:1.0.0")
97 100
 }

Loading…
Cancel
Save