使用下载管理器显示下载百分比 [英] showing download percentage with Download Manager

查看:41
本文介绍了使用下载管理器显示下载百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 android 中使用下载管理器从特定 URL 下载图像和视频.

I am using Download Manager in android to download images and Videos from a particular Url.

因此,在下载过程中,我会显示一个简单的 Progressbar.

So, When Downloading is in Progress, I am displaying a simple Progressbar.

相反,我想显示自定义进度条,它显示下载内容的进度.这意味着它应该显示一个视图,以便用户可以知道下载了多少数据.

Instead, I wanna to display custom Progressbar, which display the progress of content downloaded. That means It should display a view so that User can know that How much data is downloaded.

就像 WhatsApp 下载图片和视频一样.

Just like WhatsApp is doing with downloading images and videos.

谢谢.

推荐答案

使用下面的代码来显示下载完成的百分比如下...

used below code to show percentages download is complete like below ...

<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"
    />

并使用下面的代码来显示它...

and used below code to show it...

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

在我上面的代码中,这将调用 1 秒并每 1 秒获得一次进度增量.

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

这篇关于使用下载管理器显示下载百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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