显示进度条在下载 [英] Show Progress bar while downloading

查看:170
本文介绍了显示进度条在下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示一个进度条,显示比例完成,而我的应用程序下载一些大文件在互联网上。 是这样的:

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

推荐答案

XML文件code:

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.

这是用java文件code:

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

在我的code这将调用1秒,获得进步增加每1秒。

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

这篇关于显示进度条在下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆