我有一個按鈕,開始下載,顯示進度dailog。進度dailog有一個取消按鈕,可以停止下載。但點擊下載站,但整個應用程序崩潰時的取消,而不保留mainactivity Q)如何阻止這種情況發生,並留在MainActivity 2)如何刪除部分下載的文件 這裏是進步dailog代碼按取消下載按鈕時,整個應用程序崩潰
DownloadFileFromURL cdrt = new DownloadFileFromURL();
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type: // we set this to 0
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.setButton("Cancel", new CancelOnClickListener());
pDialog.show();
return pDialog;
default:
return null;
}
}
private final class CancelOnClickListener implements DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int which) {
MainActivity.this.finish();
dismissDialog(progress_bar_type);
if(cdrt.getStatus() == AsyncTask.Status.RUNNING){
cdrt.cancel(true);}
}
的AsyncTask
class DownloadFileFromURL extends AsyncTask<String, String, String> {
/**
* Before starting background thread
* Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
int count;
try{
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = conection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream
OutputStream output = new FileOutputStream("/sdcard/download/downloaded.jpg");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
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);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task
* Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
}
}
logcat的
12-12 11:37:10.791 25155 25155 E AndroidRuntime FATAL EXCEPTION: main
12-12 11:37:10.791 25155 25155 E AndroidRuntime Process: com.mycompany.myapp2, PID: 25155
12-12 11:37:10.791 25155 25155 E AndroidRuntime java.lang.IllegalArgumentException: no dialog with id 0 was ever shown via Activity#showDialog
12-12 11:37:10.791 25155 25155 E AndroidRuntime at android.app.Activity.missingDialog(Activity.java:3461)
12-12 11:37:10.791 25155 25155 E AndroidRuntime at android.app.Activity.dismissDialog(Activity.java:3446)
12-12 11:37:10.791 25155 25155 E AndroidRuntime at com.mycompany.myapp2.MainActivity$DownloadFileFromURL.onPostExecute(MainActivity.java:170)
12-12 11:37:10.791 25155 25155 E AndroidRuntime at com.mycompany.myapp2.MainActivity$DownloadFileFromURL.onPostExecute(MainActivity.java)
12-12 11:37:10.791 25155 25155 E AndroidRuntime at android.os.AsyncTask.finish(AsyncTask.java:636)
12-12 11:37:10.791 25155 25155 E AndroidRuntime at android.os.AsyncTask.access$500(AsyncTask.java:177)
裏面你傳遞正確的參數dismissDialog()? –