0
我正在下載AsynchTask中的pdf文件,因此它下載文件時,我的進度對話框正在更新。然而,問題是,如果我按下後退按鈕比我的警報箱彈出,下載停止在後臺。我希望即使在調用警報對話框後它仍應繼續下載,或者一旦警報彈出後暫停下載,並在單擊警報對話框的取消按鈕時繼續下載一次。即使按下後退按鈕,也不會停止在AsynchTask上
這裏是一個片段,
@Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case progress_bar_type:
pDialog = new ProgressDialog(this);
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
pDialog.setOnKeyListener(new DialogInterface.OnKeyListener()
{
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
running = false;
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
AlertDialog.setIcon(R.drawable.ic_launcher);
alertDialog.setTitle("Ariisto");
alertDialog.setMessage("Do you Want to Cancel the Download ?");
alertDialog.setCancelable(true);
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
File externalFile = new File(Environment.getExternalStorageDirectory(),"downloadedfile.pdf");
externalFile.delete();
pDialog.dismiss();
running = false;
Log.d("External File", "DELETED");
pDialog.setProgress(0);
count = 2;
}
});
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
new DownloadFileFromURL().execute(file_url);
running = true;
count = 0;
}
});
AlertDialog alert = alertDialog.create();
alert.show();
}
return false;
}
});
return pDialog;
default:
return null;
}
}
class DownloadFileFromURL extends AsyncTask<String, String, String>
{
/**
* Before starting background thread
* Show Progress Bar Dialog
* */
@SuppressWarnings("deprecation")
protected void onPreExecute()
{
super.onPreExecute();
showDialog(progress_bar_type);
publishProgress(""+(int)(0));
running = true;
}
@Override
protected void onCancelled() {
// TODO Auto-generated method stub
Log.d("------------","iNSIDE ON CANCELLED METHOD");
super.onCancelled();
}
@Override
protected String doInBackground(String... file_url)
{
// TODO Auto-generated method stub
int count;
try
{
URL url = new URL(file_url[0]);
URLConnection connection = url.openConnection();
connection.connect();
// getting file length
int lenghtOfFile = connection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream to write file
FileOutputStream output = new FileOutputStream("/sdcard/downloadedfile.pdf");
byte data[] = new byte[1024];
long total = 0;
while ( ((count = input.read(data)) != -1) && (running == true) )
{
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress(""+(int)((total*100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
output.flush();
// closing streams
output.close();
input.close();
}
catch (Exception e)
{
Log.e("Error: ", " "+e.getMessage());
}
return null;
}
protected void onProgressUpdate(String... progress)
{
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
@SuppressWarnings("deprecation")
protected void onPostExecute(String file_url)
{
// dismiss the dialog after the file was downloaded
if(running == true){
dismissDialog(progress_bar_type);
// Displaying downloaded image into image view
// Reading image path from sdcard
String imagePath = Environment.getExternalStorageDirectory().toString() + "/downloadedfile.pdf";
// setting downloaded into image view
Log.d(imagePath, "show file");
File file = new File(Environment.getExternalStorageDirectory(),"downloadedfile.pdf");
Uri external = Uri.fromFile(file);
viewPdf(external);
}
}
}
按下活動不應該停止AsyncTask,除非您取消它。儘管如此,你可能更適合使用服務。 – 323go
它不停止下載我的意思是AsynchTask仍在執行其操作。我需要它應該暫停過程當用戶按下後退按鈕,並恢復其操作,一旦用戶點擊提醒框的取消按鈕。 –
下載操作從後臺線程開始,一旦操作在doInBackground()中開始,您不能「暫停」它。點擊按鈕後,您將不得不取消該任務並重新啓動該任務。 – GamDroid