通過(guò)模擬器和真機(jī)實(shí)驗(yàn)介返,以下代碼已成功適配Andorid 10下載,適配方案在于修改文件路徑
// 下載文件的路徑
var path: String? = null
// 如果是Android 10以上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && !Environment.isExternalStorageLegacy()) {
LogUtils.e("Android 10以上設(shè)備")
path = externalCacheDir?.path
}
OkGo.get<File>("http://60.179.32.61:2811/e/file/download?code=50325988f0466427&id=91313")
.execute(object : FileCallback(path, null) {
override fun onSuccess(response: Response<File>?) {
LogUtils.e("onSuccess")
val file = response?.body()
LogUtils.e("安裝APK")
AppUtils.installApp(file)
}
override fun onError(response: Response<File>?) {
LogUtils.e("onError--->${response?.message()}")
}
override fun downloadProgress(progress: Progress?) {
LogUtils.e("${progress?.fraction}")
}
})