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

  • SEARCH
  • Home
  • 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 9223197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:01:09+00:00 2026-06-18T04:01:09+00:00

I am using Asynctask to download files in my Android app. As the file

  • 0

I am using Asynctask to download files in my Android app. As the file size is generally large, I start the file download in the background. There is no progressbar or anything shown so that the user can access other parts of the app while the download is in progress.

On completion of file download, I want to inform the user that his file has been successfully downloaded.

Using onPostexecute() method creates problems because the user is no longer on the same activity when the download process began. As a result I cannot use Alert dialog & Notifications from onPostExecute() as there are problems about the context.

I have pasted below the code for my Asynctask. So, kindly suggest me a way to fix this.

class DownloadProcess extends AsyncTask<Void,Void,Void>{

        Context cxt;

        public DownloadProcess(Context context) {
            cxt=context;
        }


        @Override
        protected Void doInBackground(Void... arg0) {
             try {
                    String fileurl="http://www.bigfiles/" + filenm;
                    URL url = new URL(fileurl); 
                    HttpURLConnection c = (HttpURLConnection) url.openConnection();
                    c.setRequestMethod("GET");
                    c.setDoOutput(true);
                    c.connect();


                    Path = Environment.getExternalStorageDirectory() + "/download/";
                    File pth=new File(Path);
                    File file = new File(pth,filenm);
                    FileOutputStream fos = new FileOutputStream(file);

                    InputStream is = c.getInputStream();

                    byte[] buffer = new byte[1024];
                    int len1 = 0;
                    while ((len1 = is.read(buffer)) != -1) {
                        fos.write(buffer, 0, len1);
                    }
                    fos.close();
                    is.close();

                } catch (IOException e) {
                    Log.d("amit", "Error: " + e);
                }

            return null;
        }


        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            if (((Activity) AppDet.this).isFinishing() == false) {      
                AlertDialog.Builder builder = new AlertDialog.Builder(AppDet.this);
                builder.setMessage("The app download is complete. Please check " +Path+ " on your device and open the " + filenm+ " file downloaded");
                builder.setCancelable(true);
                builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                    });
                AlertDialog alert = builder.create();
                alert.show();
                MediaPlayer md=MediaPlayer.create(AppDet.this, R.raw.ding);
                md.start();
            }
            else {
                //Not sure what to do here
            }
        }
  • 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-18T04:01:10+00:00Added an answer on June 18, 2026 at 4:01 am

    Use a notification (not an alert pop-up, that is bad UX) together with the application context as pointed out by @QAMAR.

    However, in our app we show a notification from an android.app.Service which manages a subset of tasks (we do not add results to the UI direct from the AsyncTask – they are liable to be killed off as your activity life cycle comes to a close, a service gives far more explicit control over when the service and it’s child tasks can end)

    final NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
    .setContentIntent(pendingIntent)
    .setSmallIcon(<some drawable>)
    .setWhen(System.currentTimeMillis())
    .setAutoCancel(true)
    .setContentTitle(<some internationalised message>)
    .setContentText(<some internationalised subtitle>);
    
    // Send the notification.
    ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(<notification id>, builder.getNotification());
    

    If you need the results of the async task to affect the activity you are working with then bound services is what you need. I’m about to delve into these today so will post back with any insights I have…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using AsyncTask to download files. There i implemented progress: //Updating progress dialog
I am using a AsyncTask to download a file from a URL in Android.
I am trying to download multiple file using AsyncTask. I start each download in
I am using this code (inside an AsyncTask) to download files: URL u =
I'm using an AsyncTask class to download data from a php file. After downloading,
I'm using AsyncTask to download some files, and want to do something after all
I'a using a asynctask to download file. It works normally until i turn off
I am using an AsyncTask to download a file, with the purpose of measuring
I want to download an mp3 file using an AsyncTask or a thread. How
I am trying to download a 600 MB file using an AsyncTask. If I

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.