Show Progress bar while downloading

前端 未结 4 1841
太阳男子
太阳男子 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:

     
    

    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.

提交回复
热议问题