0

このダイアログをユーザーに表示するファイルをダウンロードするとき、キャンセルボタンがあります

    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;

    }

私の問題は:

  • ダイアログファイルのダウンロードでキャンセルボタンを押してもキャンセルしないでください

  • ファイルのダウンロードが完了した場合、またはファイルを正確に削除しない場合に、ユーザーがキャンセルボタンを押したときに実行したい
4

1 に答える 1

3

ダイアログファイルのダウンロードでキャンセルボタンを押してもキャンセルしないでください

キャンセルボタンをクリックしてAsyncTaskをキャンセルするには、 AsyncTask.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();
                        }
                    });

ファイルのダウンロードが完了した場合、またはファイルを正確に削除しない場合に、ユーザーがキャンセルボタンを押したときに実行したい

doInBackground次のようにファイルのダウンロードを停止するには、AsyncTaskが実行されているか、内部でキャンセルされているかを確認する必要があります。

    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 に答える