From 24c0c29e959d9f8b6ad8bcea773d6c431558f6d7 Mon Sep 17 00:00:00 2001 From: tangwanchao <18284228392@163.com> Date: Thu, 2 Feb 2023 17:40:57 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20=E4=B8=8B=E8=BD=BD=E8=BF=9B=E5=BA=A6?= =?UTF-8?q?=E5=9B=9E=E8=B0=83=E5=86=8D=E4=B8=BB=E7=BA=BF=E7=A8=8B=E4=B8=AD?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/me/twc/source/download/DownloadCallback.kt | 3 +++ download/src/main/java/me/twc/source/download/Downloader.kt | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/download/src/main/java/me/twc/source/download/DownloadCallback.kt b/download/src/main/java/me/twc/source/download/DownloadCallback.kt index 3171578..0d33af2 100644 --- a/download/src/main/java/me/twc/source/download/DownloadCallback.kt +++ b/download/src/main/java/me/twc/source/download/DownloadCallback.kt @@ -1,5 +1,6 @@ package me.twc.source.download +import androidx.annotation.MainThread import okhttp3.ResponseBody import java.io.File @@ -31,11 +32,13 @@ interface DownloadCallback { /** * 开始下载 */ + @MainThread fun onStart() {} /** * 下载中 */ + @MainThread fun onDownload(downloadLength: Int, totalLength: Long) {} /** diff --git a/download/src/main/java/me/twc/source/download/Downloader.kt b/download/src/main/java/me/twc/source/download/Downloader.kt index d35163c..eb9e0e3 100644 --- a/download/src/main/java/me/twc/source/download/Downloader.kt +++ b/download/src/main/java/me/twc/source/download/Downloader.kt @@ -26,7 +26,9 @@ class Downloader( bs = responseBody.byteStream() outputFile = callback.genSaveFile() ?: genSaveFile() fos = FileOutputStream(outputFile) - callback.onStart() + withContext(Dispatchers.Main){ + callback.onStart() + } val totalLength = responseBody.contentLength() var downloadLength = 0 val buffer = ByteArray(8 * 1024)