我想从doInBackground更新对话框的下载进度.
我正在打印日志以及发布进度.
他们俩都没有工作.
最后更新对话框,并一次打印所有日志值
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(); }}
解决方法:
从onProgressUpdate移除super,然后尝试
@OverrIDeprotected voID onProgressUpdate(VoID... values) { //super.onProgressUpdate(values); mProgressDialog.setProgress(progress);}
如果不起作用,则可以在循环中添加sleep语句,以便该循环释放处理器并留出时间发布进度
try { Thread.sleep(200);} catch (InterruptedException e) { e.printstacktrace();}
总结 以上是内存溢出为你收集整理的Java-无法在后台的while循环中发布异步任务的进度-Android全部内容,希望文章能够帮你解决Java-无法在后台的while循环中发布异步任务的进度-Android所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)