fix: OTA 弹窗进度条修复

1. 进度条宽度用 post{} 延迟设置,确保容器已布局(width>0)
2. 进度回调限频 200ms,避免主线程过载
3. 用 runOnUiThread 替代 launch(Main),更简单可靠
4. 各状态切换加日志

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
dongliang
2026-04-30 21:42:06 +09:30
parent acd262a3a6
commit 3ced3c74cf
2 changed files with 36 additions and 10 deletions

View File

@@ -209,10 +209,13 @@ class MainActivity : AppCompatActivity() {
/** 待下载的更新 URL */
private var pendingUpdateUrl: String? = null
/** 上次进度更新时间(限制频率,避免主线程过载) */
private var lastProgressUpdate = 0L
/** 开始下载并安装 APK */
private fun startDownloadAndInstall() {
val url = pendingUpdateUrl ?: return
Timber.d("OTA: 开始下载安装流程, url=%s", url)
updateManager.isUpdating = true
// 保持屏幕常亮
screenController.turnOn()
@@ -221,19 +224,23 @@ class MainActivity : AppCompatActivity() {
activityScope.launch {
val file = updateManager.downloadApk(url) { progress, bytes ->
// 进度回调在 IO 线程,切回主线程更新 UI
launch(kotlinx.coroutines.Dispatchers.Main) {
updateDialog.updateProgress(progress, bytes)
// 限制进度更新频率:最多每 200ms 更新一次 UI
val now = System.currentTimeMillis()
if (now - lastProgressUpdate >= 200 || progress >= 100) {
lastProgressUpdate = now
runOnUiThread {
updateDialog.updateProgress(progress, bytes)
}
}
}
// 回到主线程处理结果
kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.Main) {
if (file != null) {
// 下载成功 → 触发安装
Timber.d("OTA: 下载完成,触发安装")
updateManager.installApk(file)
} else {
// 下载失败 → 显示错误
Timber.d("OTA: 下载失败")
updateManager.isUpdating = false
updateDialog.showError()
}