AsyncTask的进度和 [英] AsyncTask and Progressbar
问题描述
我写的,我下载的PDF文件逐一展示他们在一个视图的应用程序。
视图有下一个按钮,它的onClick显示PDF(已下载的previous一次单击),并下载下一PDF再次单击Next按钮时将被显示。我下载使用AsyncTasks文件。
我有一个AsyncTask的和里面一个ProgressDialog为每个文件。
如果用户点击一个按钮,如果该文件显示(即本来应该在previous接下来点击下载)仍然在下载,我想说明的进度一直等到下载完毕。以下是我的code的片段。
I am writing an application in which I download PDFs one by one and show them in a view. The view has a next button which onClick shows the PDF(that was downloaded on the previous next click) and downloads the next PDF which will be shown when the next button is clicked again. I am downloading the files using AsyncTasks. I have one AsyncTask and a ProgressDialog inside it for each file. If a user clicks next button and if the file to show(that was supposed to be downloaded on previous next click) is still downloading, I want to show the progressbar and wait till the download is complete. Following are the snippets of my code..
public void saveFile(String uri, int index)
{
if(!f.exists())
{
Downloader downloader = new Downloader(f, index);
//adding the downloader to the list
downloaders.add(downloader);
downloader.execute(uri);
}
}
下面是我在其中显示PDF的方法
here is the method in which I show the PDF
private void showPdf(){
[...]
if(!completed[to_show]){
downloaders.get(to_show).mProgressDialog.show();
}
[...]
}
和这里是我的AsyncTask code
and here is my AsyncTask code
private class Downloader extends AsyncTask<String, Integer, String> {
ProgressDialog mProgressDialog;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.show();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
completed[fileNum] = true;
Log.e("PDFReaderAct",String.valueOf(fileNum)+" download complete");
mProgressDialog.dismiss();
}
public Downloader() {
[...]
mProgressDialog = new ProgressDialog(PDFReaderAct.this);
[...]
}
@Override
protected String doInBackground(String...params ) {
try
{
//Code to download the file
publishProgress((int) (total * 100 / fileLength));
os.write(bytes, 0, count);
}
[...]
}
}
@Override
protected void onProgressUpdate(final Integer... progress) {
super.onProgressUpdate(progress);
runOnUiThread( new Runnable() {
public void run() {
// TODO Auto-generated method stub
mProgressDialog.setProgress(progress[0]);
}
});
}
}
当我运行这个code,进度条显示出来,但showPdf后(执行)的方法。我跟踪了AsyncTasks和progressDialogs为每个文件下载。我怎样才能显示previous下载的进度。
When I run this code, the progressbar shows up but after the showPdf() method is executed. I am keeping track of the AsyncTasks and progressDialogs for each file download. How can I show the progress of the previous download..
谢谢,
推荐答案
在这里
private void showPdf(){
[...]
downloaders.get(to_show).mProgressDialog.show(); //<<get method of AsyncTask
[...]
}
如DOC约<一个href=\"http://developer.android.com/reference/android/os/AsyncTask.html#get%28long,%20java.util.concurrent.TimeUnit%29\"相对=nofollow> AsyncTask的。得到(长超时,单位的TimeUnit):
等待如果必要,最多给定的时间为使计算
完成,然后获取其结果。
Waits if necessary for at most the given time for the computation to complete, and then retrieves its result.
意味着如果你用这种方法获取结果回从AsyncTask的到你的UI主线程然后它会阻止你的主UI执行,直到结果不退换形式的AsyncTask的doInBackground方法
means if you use this method for getting result back from AsyncTask to your UI main Thread then it will stop your main UI execution until result not returned form AsyncTask's doInBackground method
解决方案是使用 onPostExecute
更新UI元素时AsyncTask的执行完整的
solution is use onPostExecute
for updating UI elements when AsyncTask execution complete
这篇关于AsyncTask的进度和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!