Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用ProgressDialog下载文件,取消下载(Android)_Java_Android_Download_Progressdialog - Fatal编程技术网

Java 使用ProgressDialog下载文件,取消下载(Android)

Java 使用ProgressDialog下载文件,取消下载(Android),java,android,download,progressdialog,Java,Android,Download,Progressdialog,当下载文件时,我向用户显示这个对话框,有取消按钮 protected Dialog onCreateDialog(int id) { switch (id) { case DIALOG_DOWNLOAD_PROGRESS: mProgressDialog = new ProgressDialog(this); mProgressDialog.setMessage("Downloading .."); mProgressDia

当下载文件时,我向用户显示这个对话框,有取消按钮

    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;
    }
}

我的下载代码是:

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;

    }

我的问题是:
  • 按对话框上的“取消”按钮下载文件时,不要取消
  • 如果文件下载完成,我想在用户按下“取消”按钮时删除
    或者不完全删除文件
按对话框上的“取消”按钮下载文件时,不要取消

您需要在“取消”按钮上使用以取消异步任务,单击方式为:

   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();
                        }
                    });
按对话框上的“取消”按钮下载文件时,不要取消

您需要在“取消”按钮上使用以取消异步任务,单击方式为:

   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();
                        }
                    });

你已经试过了,对吗?@njzk2在另一种方法中,我做了,你也试过了,对吗?@njzk2在另一种方法中,我完全下载进程,我只想删除已取消file@SaeedHashemi:您对我的回答有任何问题,因为如果下载取消,您可以删除其他部分中的文件?@SaeedHashemi:如果您有任何问题,请使用布尔变量检查AsyncTask的状态我只想删除已取消的文件file@SaeedHashemi:您对我的答案有任何问题,因为如果下载取消,您可以删除其他部分中的文件?@SaeedHashemi:如果您有任何问题,请使用布尔变量检查AsyncTask的状态