问题
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);
}
}
here is the method in which I show the PDF
private void showPdf(){
[...]
if(!completed[to_show]){
downloaders.get(to_show).mProgressDialog.show();
}
[...]
}
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]);
}
});
}
}
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..
Thanks,
回答1:
here
private void showPdf(){
[...]
downloaders.get(to_show).mProgressDialog.show(); //<<get method of AsyncTask
[...]
}
as doc as about AsyncTask. get (long timeout, TimeUnit unit) :
Waits if necessary for at most the given time for the computation to complete, and then retrieves its result.
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
solution is use onPostExecute
for updating UI elements when AsyncTask execution complete
来源:https://stackoverflow.com/questions/13908485/asynctask-and-progressbar