I am building an android app in which I need to download a pdf and then view it. I have the downloading and viewing code working fine. I decided to use an Asynctask to download and show the progress. However, when it is launched, the system appears to have frozen and the dialog box doesn’t come up until after the file has been downloaded. This is my onClick code:
public void onClick(View v) {
DownloadFile downloadFile = new DownloadFile();
downloadFile.execute(
"http://store.aqa.org.uk/qual/newgcse/pdf/AQA-4130-W-SP.PDF",
"userguide.pdf");
try {
downloadFile.get();
viewPDF(getExternalFilesDir(null) + "/userguide.pdf");
} catch (Exception e) {
}
}
Any idea how I could resolve this.
I have considered that I can put the viewPdf in the onPostExecute method, but this method is specifically for downloading files and this is a special case with which the pdf has to be viewed.
Thanks in advance
Edit: I solved my problem.
I added a parameter in which I would specify the of the request. This gets included in result and dependent upon it, it might get shown or not. Thanks for all the help
as doc says
AsyncTask.html.get() :
means if you call get method for getting result from AsyncTask this will make wait in UI thread until control back from AsyncTask doInBackground method.
you can solve current issue by passing Current Activity Context to DownloadFile for Accessing method’s from Activity to onPostExecute method as :
and execute
DownloadFileon Button click as from Activity :