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
 import android.app.Notification
3
 import android.app.Notification
4
 import android.app.NotificationChannel
4
 import android.app.NotificationChannel
5
 import android.app.NotificationManager
5
 import android.app.NotificationManager
6
+import android.app.Service
6
 import android.content.ComponentName
7
 import android.content.ComponentName
7
 import android.content.Context
8
 import android.content.Context
8
 import android.content.Intent
9
 import android.content.Intent
9
 import android.os.Build
10
 import android.os.Build
11
+import android.os.Environment
10
 import android.widget.Toast
12
 import android.widget.Toast
13
+import com.billbook.lib.downloader.Download
14
+import com.billbook.lib.downloader.Downloader
11
 import com.xhly.corelib.Const
15
 import com.xhly.corelib.Const
12
 import com.xhly.corelib.bean.AppInfo
16
 import com.xhly.corelib.bean.AppInfo
13
 import com.xhly.corelib.eventbus.UIEvent
17
 import com.xhly.corelib.eventbus.UIEvent
27
 import com.xhly.websocket.service.WebSocketHelper
31
 import com.xhly.websocket.service.WebSocketHelper
28
 import com.xhly.websocket.utils.SocketPushUtils.dealPushMessage
32
 import com.xhly.websocket.utils.SocketPushUtils.dealPushMessage
29
 import kotlinx.coroutines.CoroutineScope
33
 import kotlinx.coroutines.CoroutineScope
34
+import kotlinx.coroutines.Dispatchers
30
 import kotlinx.coroutines.MainScope
35
 import kotlinx.coroutines.MainScope
31
 import kotlinx.coroutines.cancel
36
 import kotlinx.coroutines.cancel
32
 import kotlinx.coroutines.launch
37
 import kotlinx.coroutines.launch
38
+import kotlinx.coroutines.withContext
33
 import org.greenrobot.eventbus.EventBus
39
 import org.greenrobot.eventbus.EventBus
34
 import org.greenrobot.eventbus.Subscribe
40
 import org.greenrobot.eventbus.Subscribe
35
 import org.greenrobot.eventbus.ThreadMode
41
 import org.greenrobot.eventbus.ThreadMode
42
+import java.io.File
36
 import java.util.UUID
43
 import java.util.UUID
37
 
44
 
38
 open class AppSocketService : LongConnService() {
45
 open class AppSocketService : LongConnService() {
65
         /* startNotification()*/
72
         /* startNotification()*/
66
         LogShow("已经创建")
73
         LogShow("已经创建")
67
         appList = AppUtils.GetAppList(this) as ArrayList<AppInfo>
74
         appList = AppUtils.GetAppList(this) as ArrayList<AppInfo>
75
+        //downLoadFile("")
68
     }
76
     }
69
 
77
 
70
-
71
     override fun processTextMessage(text: String) {
78
     override fun processTextMessage(text: String) {
72
         //Log.v("websocket收到的消息", text);
79
         //Log.v("websocket收到的消息", text);
73
         dealPushMessage(text)
80
         dealPushMessage(text)
207
         intent.setPackage("com.xhly.manageapp")
214
         intent.setPackage("com.xhly.manageapp")
208
         intent.setAction("test")
215
         intent.setAction("test")
209
         sendBroadcast(intent)
216
         sendBroadcast(intent)
210
-        LogShow("已经销毁")
217
+        LogShow("Service被销毁")
211
     }
218
     }
212
 
219
 
213
     private fun startNotification() {
220
     private fun startNotification() {
223
             startForeground(1, notification)
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
 
94
 
95
     //圆角imageview
95
     //圆角imageview
96
     api("com.makeramen:roundedimageview:2.3.0")
96
     api("com.makeramen:roundedimageview:2.3.0")
97
+
98
+    //下载框架
99
+    api("io.github.ydxlt:okdownloader:1.0.0")
97
 }
100
 }

Loading…
Cancel
Save