下载完成后无法关闭对话框

时间:2014-03-16 04:58:34

标签: android android-asynctask

以下代码正常工作并下载音乐文件,但我遇到了一个奇怪的问题:在某些情况下,下载完成后对话框不会被解除,对话框会停止。

我该怎么办?

我应该在代码中添加什么内容?

class DownloadFileFromURL extends AsyncTask<String, String, String> {
@Override
protected void onCancelled() {
    File file= new File("/sdcard/EBKH/samat.mp3");
    file.delete();
}
@Override
protected void onPreExecute() {
    super.onPreExecute();
    showDialog(progress_bar_type);
}
@Override
protected String doInBackground(String... f_url) {
    int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
int lenghtOfFile = conection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(), 8192);
OutputStream output = new FileOutputStream("/sdcard/EBKH/samat.mp3");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
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]));
}
@Override
protected void onPostExecute(String file_url) {
    // dismiss the dialog after the file was downloaded
    dismissDialog(progress_bar_type);
    if(audioFile.exists())
    {
    Builder alert = new AlertDialog.Builder( DoaMatn2.this);
alert.setMessage("....");
alert.setPositiveButton("OK", null);
alert.show();
}}}}

和对话框代码:

@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type:
    pDialog = new ProgressDialog(this);
    pDialog.setMessage("در حال دانلود،لطفا صبور باشید...");
    pDialog.setIndeterminate(false);
    pDialog.setMax(100);
    pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    pDialog.setCancelable(false);
    pDialog.show();
    return pDialog;
default:
    return null;
}

并关闭对话框:

@Override
protected void onPostExecute(String file_url) {
    // dismiss the dialog after the file was downloaded
    dismissDialog(progress_bar_type);
    if(audioFile.exists())
    {
    Builder alert = new AlertDialog.Builder( DoaMatn2.this);
            alert.setMessage("....");
            alert.setPositiveButton("OK", null);
            alert.show();
              }

0 个答案:

没有答案