次のコードを使用して、選択したPDFのリストからPDFをダウンロードしています。ダウンロードしたPDFを開きたいです。問題は、ダウンロードが完了する前にPDFを開くためのコードが発生することです。ダウンロードが完了するまでPDFを開くコードが実行されないようにするにはどうすればよいですか.....
注:PDFを元々text / htmlとして読んでいる理由は、元々PDFをWebサイトのURLとして持っていて、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();
}
}
}
//上記のコードがインターネットからPDFのダウンロードを完了するまで、次のコードを実行したくありません。
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();
}
}
}