Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9183925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:55:18+00:00 2026-06-17T18:55:18+00:00

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

  • 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:55:19+00:00Added an answer on June 17, 2026 at 6:55 pm

    when press cancel button on dialog file downloading don’t cancel

    you will need to use AsyncTask.cancel() for canceling AsyncTask on cancel button click as:

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

    I want to when user press cancel button if file downloaded complete or
    not delete file exactly

    you will need to check is AsyncTask running or canceled inside doInBackground to stop file downloading as :

        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;
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a button to download an excel file. When the user clicks the
Currently to have to user download a file I have the following javascript. document.location.href
I have a page that allows the user to download a dynamically-generated file. It
I have a web page which a user can download a PDF file via
I cannot download file using java if the url contains special characters. eg:-http://something.com/something/this+this+this.html http://something.com/something/this%20this%20this.html
i have a link <a id=DownloadLink href='controller/action' target=_blank>Download File</a> that has to open a
Hey! This is what I have already done so far which works great: Download
I need to show a progress bar to the user who requests a file
I write a program support download file. When in this download activity, I start
I have a file downloading android application.Here file is downloaded when user click on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.