gpt4 book ai didi

android - Android下载管理器如何修改更新间隔

转载 作者:行者123 更新时间:2023-12-05 07:12:13 26 4
gpt4 key购买 nike

我尝试使用 Android downloadManager 从服务器获取文件,并确认它可以正常工作。

但是,COLUMN_BYTES_DOWNLOADED_SO_FAR 值大约每 1 到 2 秒更改一次,因此程序栏不会平滑增加。有什么办法可以修改更新间隔吗?

boolean downloading = true;
int oldProgressValue = 0;
int lastProgressValue = 0;
long startTime = System.currentTimeMillis();

try {
while (downloading) {
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(downloadId);
Cursor cursor = downloadManager.query(q);
cursor.moveToFirst();
int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));

cursor.moveToFirst();
int bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));

if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
downloading = false;
}

//0 ~ 100
lastProgressValue = (int) ((bytes_downloaded * 100l) / bytes_total);

Thread.sleep(50);

if (timeOut > 0) {
downloading = UpdateTimeOut(startTime, oldProgressValue, lastProgressValue);
}

if (oldProgressValue != lastProgressValue) {
oldProgressValue = lastProgressValue;
int finalOldProgressValue = oldProgressValue;
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(String.valueOf(finalOldProgressValue));
}
});
}
cursor.close();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {

}

另外

Android Download Manager with progress dialog

同样的问题,我尝试作为答案,但没有解决。

Android DownloadManager Progress

我也应用了问题的答案,但没有得到更新的值。

最佳答案

我遇到了同样的问题,我所做的是每次 COLUMN_BYTES_DOWNLOADED_SO_FAR 更改时调用此方法来设置 ValueAnimator,这使它看起来很流畅

   private fun updateProgressBar(progress: Int){
runOnUiThread {
val progressAnimator = ValueAnimator.ofFloat(pb_download_video.progress.toFloat(), progress.toFloat())
progressAnimator.duration = 300
progressAnimator.addUpdateListener {
val value: Int = (it.animatedValue as Float).toInt()
binding.pbDownloadVideo.progress = value
}
progressAnimator.doOnEnd { if (progress == 100) // action when is done loading }
progressAnimator.start()
}
}

关于android - Android下载管理器如何修改更新间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60518431/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com