Not able to publish progress from async task do in background#39;s while loop - Android(无法在后台的 while 循环中发布异步任务的进度 - Android)
问题描述
我想从 doInBackground 更新对话框的下载进度.
我正在打印日志以及发布进度.
他们都没有工作.
I want to update dialog's download progress from doInBackground.
I am printing log as well as publishing progress.
Neither of them are working.
它最后更新对话框并在最后一次打印所有日志值
private class DownloadEReport extends AsyncTask<String, Void, Void> {
int progress = 0;
protected void onPreExecute() {
mProgressDialog = new ProgressDialog(EReport.this);
mProgressDialog.setTitle("Downloads");
mProgressDialog.setMessage("Downloading, Please Wait!");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
mProgressDialog.setProgress(progress);
}
@Override
protected Void doInBackground(String... strings) {
String mUrl = strings[0];
String json = "";
int count;
try {
URL url = new URL(mUrl);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream to write file
OutputStream output = new FileOutputStream("/sdcard/downloadedfile.txt");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// writing data to file
output.write(data, 0, count);
// publishing the progress....
// After this onProgressUpdate will be called
Log.e("JSON Download Progress", "" + (int) ((total * 100) / lenghtOfFile));
progress = (int) (total * 100 / lenghtOfFile);
publishProgress();
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
mProgressDialog.dismiss();
}
}
推荐答案
对这种行为的一种可能解释是从远程输入流读取并将其写入输出缓冲区的速度非常快.我尝试了相同的代码,但只运行了 10 次循环.
One possible explanation for the behaviour is that reading from remote input stream and writing it into output buffer is extremely fast. I tried the same code but with just a loop running for 10 times.
int total = 0;
while(total <= 100){
progress = total;
total += 10;
publishProgress();
}
即使一开始我也看不到进度对话框,所以在循环中放置一个 Thread.sleep() 并且进度对话框工作得很好.
Even I could not see the progress dialog at first, so put a Thread.sleep() in the loop and the progress dialog works just fine.
int total = 0;
while(total <= 100){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
progress = total;
total += 10;
publishProgress();
}
尝试记录写入输出缓冲区的时间 (System.currentTimeMillis()
).
Try to log the time (System.currentTimeMillis()
) at which you are writing into the output buffer.
希望有帮助.
这篇关于无法在后台的 while 循环中发布异步任务的进度 - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法在后台的 while 循环中发布异步任务的进度
基础教程推荐
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01