【问题标题】:showing download percentage with Download Manager [duplicate]使用下载管理器显示下载百分比[重复]
【发布时间】:2018-08-20 04:25:37
【问题描述】:

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

所以,当下载正在进行时,我会显示一个简单的Progressbar

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

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

谢谢。

【问题讨论】:

  • 在发布关于 SO 的新问题之前,请先 google。请通过this 发帖

标签: android android-progressbar android-download-manager


【解决方案1】:

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

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

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

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 秒获得一次进度增量。

【讨论】:

    猜你喜欢
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多