I'm using the following code to download a pdf from a list of pdfs depending on which is selected. I want to then open the pdf downloaded. The problem is that the code to open the pdf occurs before the download is finished. How do I make it so that the code to open the pdf doesn't run until the download is finished.....
Note: the reason i'm reading in the pdf originally as a text/html is because I originally have the pdf as a website url and then it automatically downloads when opened in a url.
public class pdfSelectedListener implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent,
View view, int pos, long id) {
String pdfName = "";
for(int i=0;i<nameList.size();i++){
if(nameList.get(i).equals(parent.getItemAtPosition(pos).toString())){
try{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(websiteList.get(i)), "text/html");
int slashIndex = websiteList.get(i).lastIndexOf('/');
pdfName = websiteList.get(i).substring(slashIndex+1, websiteList.get(i).length());
startActivity(intent);
}catch(Exception e){
Toast.makeText(PDFActivity.this, "Invalid link.", Toast.LENGTH_LONG).show();
}
}
}
//I dont want the the following code to excute until the above code is done downloading the pdf from the internet.
File file = new File("/mnt/sdcard/Download/"+pdfName);
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(PDFActivity.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(PDFActivity.this,
"File doesn't exist.",
Toast.LENGTH_SHORT).show();
}
}
}