0

When download file I show to user this dialog ,have cancel button

    protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DIALOG_DOWNLOAD_PROGRESS:
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setMessage("Downloading ..");
        mProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
                "Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
        return mProgressDialog;
    default:
        return null;
    }
}

and my downloading code is :

protected String doInBackground(String... aurl) {
        int count;

        try {

            URL url = new URL(aurl[0]);
            URLConnection conexion = url.openConnection();
            conexion.connect();

            int lenghtOfFile = conexion.getContentLength();
            Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream(sunDir.getPath()
                    + musicFileName);
            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) {
        }
        return null;

    }

my problem is :

  • when press cancel button on dialog file downloading don't cancel
  • I want to when user press cancel button if file downloaded complete
    or not delete file exactly
4

1 回答 1

3

在对话框文件下载上按取消按钮时不要取消

您将需要使用AsyncTask.cancel()在取消按钮单击时取消 AsyncTask:

   public static boolean downloadstatus=true;
    mProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
                    "Cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            your_AsyncTask.cancel(true);  //<<<<<
                             downloadstatus=false;
                            dialog.cancel();
                        }
                    });

如果文件下载完成或没有完全删除文件,我想当用户按下取消按钮时

您需要检查 AsyncTask 是在运行还是在内部取消doInBackground以停止文件下载:

    while ((count = input.read(data)) != -1) {
       if(!your_AsyncTask.isCancelled() &&  downloadstatus !=false){
        total += count;
        publishProgress("" + (int) ((total * 100) / lenghtOfFile));
        output.write(data, 0, count);
       }
      else{
             // free all resources here
            break;
        }
    }
于 2013-01-23T15:36:00.183 回答