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 8080693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:29:35+00:00 2026-06-05T16:29:35+00:00

i try to put the progress bar into the notification. but the problem is

  • 0

i try to put the progress bar into the notification. but the problem is the system will not respond for few seconds and progress bar stuck at beginning once it’s started, please take note that even the progress bar is get stuck-ed but the file is still downloading.

Below is the code, please guide me how to deal with the progress bar.

public class StaffChoice extends Activity {
    public static final int DIALOG_DOWNLOAD_PROGRESS = 0;

    ProgressBar progressBar;
    private int progress = 10;

    Intent MyI;
    PendingIntent MyPI;
    NotificationManager MyNM;
    Notification notification;
    NotificationManager notificationManager;

    private void startDownload() {
        String url = "http://www.domainurl.com/3d.png";     //this is not a valid url
        DownloadFileAsync dfa = new DownloadFileAsync();
        dfa.setActivity(StaffChoice.this);
        dfa.execute(url);
        }

       public void onTaskCompleted() {
           dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
       }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.staffchoices);

        MyI = new Intent(getApplicationContext(), MaxAppsAct.class);
        MyPI = PendingIntent.getActivity(getApplicationContext(), 0, MyI, 0);
        MyNM = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

        Intent intent = new Intent(this, StaffChoice.class);
        final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);

        notification = new Notification(R.drawable.logo, "Downloading...", System.currentTimeMillis());
        notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
        notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.staffchoices);
        notification.contentIntent = pendingIntent;
        notification.contentView.setImageViewResource(R.id.imgIcon, R.drawable.save);
        notification.contentView.setTextViewText(R.id.tvText, "Downloading...");
        notification.contentView.setProgressBar(R.id.pbStatus, 100, progress, false);

        notificationManager = (NotificationManager) getApplicationContext().getSystemService(getApplicationContext().NOTIFICATION_SERVICE);

        notificationManager.notify(42, notification);        
        startDownload();
        }

    public class DownloadFileAsync extends AsyncTask<String, String, String> {
        StaffChoice activity;
        private boolean completed;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog(DIALOG_DOWNLOAD_PROGRESS);
            }

        @Override
        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);
            File folder = new File(Environment.getExternalStorageDirectory() + "/MaxApps");
            boolean success = false;
            if (!folder.exists()) {
                success = folder.mkdirs();
                }
            if (!success) {

            } else {

            }

            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream("/sdcard/MaxApps/3d.png");

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

        protected void onProgressUpdate(Integer... progress) {
            notification.contentView.setProgressBar(R.id.pbStatus, 100, progress[0], false);
            notificationManager.notify(42, notification);
            }

        @Override
        protected void onPostExecute(String unused) {
            completed = true;
            notifyActivityTaskCompleted();
            }

        public void setActivity(StaffChoice activity){
            this.activity = activity;
            if (completed) {
                notifyActivityTaskCompleted();
                }
            }

        private void notifyActivityTaskCompleted() {
            if (null != activity) {
                activity.onTaskCompleted();
                }
            }
        }
    }

Logcat:

  • 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-05T16:29:37+00:00Added an answer on June 5, 2026 at 4:29 pm

    I recommend you shouldn’t use AsyncTask inside a thread.

    To show notification, in your AysnTask, add following code:

     @Override
     protected void onProgressUpdate(Integer... progress) {
         notification.contentView.setProgressBar(R.id.pbStatus, 100, progress[0], false);
         notificationManager.notify(42, notification);
     }
    

    Update: error in the logcat look like this (the dismiss dialog cause error):
    http://blog.doityourselfandroid.com/2010/11/14/handling-progress-dialogs-and-screen-orientation-changes/

    You can try this:

    class YourActivity extends Activity {
    
    private void startDownload() {
    String url = "http://www.domainurl.com/3d.png";
    DownloadFileAsync dfa = new DownloadFileAsync();
    dfa.setActivity(YourActivity.this);
    dfa.execute(url);
     }
    
       public void onTaskCompleted() {
    dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
       }
    
       public void dismissDialog(int id){
        //...
       }
    
       public void showDialog(int id){
        //...
       }
    
    public class DownloadFileAsync extends AsyncTask<String, String, String> {
    
    YourActivity activity;
        private boolean completed;
    
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        activity.showDialog(DIALOG_DOWNLOAD_PROGRESS);
    }
    
    @Override
    protected String doInBackground(String... aurl) {
        // nothing change
    }
    
    protected void onProgressUpdate(Integer... progress) {
        notification.contentView.setProgressBar(R.id.pbStatus, 100, progress[0], false);
        notificationManager.notify(42, notification);
    }
    
    @Override
    protected void onPostExecute(String unused) {
            completed = true;
            notifyActivityTaskCompleted();
    
    }
    }
    
    public void setActivity(YourActivity activity){
               this.activity = activity;
               if ( completed ) {
                notifyActivityTaskCompleted();
               }
    
    private void notifyActivityTaskCompleted() {
    if (null != activity) {
        activity.onTaskCompleted();
    }
    
    }
    }
    

    When you use publishProgress in doInBackground, it should update the progress to notification

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

Sidebar

Related Questions

I try to put an event on document loading but not works... I'd put
I try to put sixteen checkbuttons into frame, placing them into four columns like:
When I try to put a value into a DATE field which is invalid,
I try to put some strings to CharBuffer with CharBuffer.put() function but the buffer
I try to put a branching diagram into lists and sublists layout is msg01
I have a problem when I try put data from controller to form view
please i try to put the content of two NSArray in two NSMutableArray but
Actually i have a problen while using progress dialog.The problem is not with the
I would like to show progress bar in my swing app, which will zip
I will try to put it in this way: I'm making a sum :

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.