Show Progress bar while downloading

前端 未结 4 1801
太阳男子
太阳男子 2021-01-31 12:42

I want to show a progress bar showing percentage completed while my app is downloading some big files over the internet. something like this :

相关标签:
4条回答
  • 2021-01-31 13:19

    xml file code:

     <ProgressBar
            android:id="@+id/xml_reg_progressBar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/common_left_right_margin"
            android:layout_marginRight="@dimen/common_left_right_margin"
            android:layout_marginTop="@dimen/reg_ph_linear_top_margin"
            />
    

    you can change progressbar style as per your requirement.

    this is java file code:

     protected static final int TIMER_RUNTIME = 180000; // in ms --> 10s
        private ProgressBar progressTimer;
        public void startTimer() {
                final Thread timerThread = new Thread() {
                    @Override
                    public void run() {
                        mbActive = true;
                        try {
                            int waited = 0;
                            while (mbActive && (waited < TIMER_RUNTIME)) {
                                sleep(1000);
                                if (mbActive) {
                                    waited += 1000;
    
                                    updateProgress(waited);
                                }
                            }
                        } catch (InterruptedException e) {
                            // do nothing
                        } finally {
                            onContinue();
                        }
                    }
    
                };
                timerThread.start();
    
            }
    
            Handler handler = new Handler();
    
            private void onContinue() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
    
                        txtCallContactUsNumber
                                .setText(CommonVariable.CallForSupporMessage
                                        + contactInfo.MobileNo);
    
                    }
                });
    
            }
    
            public void updateProgress(final int timePassed) {
                if (null != progressTimer) {
                    // Ignore rounding error here
                    final int progress = progressTimer.getMax() * timePassed
                            / TIMER_RUNTIME;
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Log.i("timePassed", "timePassed=" + timePassed);
    
                            DateFormat formatter = new SimpleDateFormat("mm:ss");
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTimeInMillis(timePassed);
                            String timer = formatter.format(calendar.getTime());
                            formatter.setTimeZone(java.util.TimeZone.getTimeZone("GMT"));
                            txtTimerCount.setText(formatter.format(timePassed));
                        }
                    });
    
                    progressTimer.setProgress(progress);
                }
            }
    

    in my code this will call 1 second and get progress increment every 1 second.

    0 讨论(0)
  • 2021-01-31 13:25

    Check these links:

    http://examples.javacodegeeks.com/android/core/ui/progressdialog/android-progressdialog-example/

    Show ProgressDialog Android

    Clearly, explains how to implement the ProgressBar or ProgressDialog

    Here's a good library for customized ProgressDialog:

    https://github.com/passsy/android-HoloCircularProgressBar

    https://github.com/Todd-Davies/ProgressWheel

    0 讨论(0)
  • 2021-01-31 13:34

    You can acheive this by using a progress bar in two ways.

    1.Add a progress bar(the circle one) to act as a loading indicator.

    To do this simple show a progress bar on start of the image download thread progressbar.show(); After the download is finished dismiss the progressbar progressbar.dismiss();

    You can use AsyncTask for this

    Refer this link

    2.Add a progress bar(the rectangular bar type) to show the progress of the download or what ever it may be.

    I would recomend you to go through this link..

    0 讨论(0)
  • 2021-01-31 13:37

    You should be using Progress bar. I assume for downloading file you are using Asynctask, if you are not then i recommend you to do that. Async task has has two more cool methods apart from the

    doInBackground()

    They are

    preExecute and postExecute

    You can override these method, now create a simple progress bar and start it in preExecute and end it in postExecute method. Here is the working LINK

    0 讨论(0)
提交回复
热议问题