如何为下载提供进度条?

发布于 2024-10-21 16:27:32 字数 92 浏览 3 评论 0原文

在我的应用程序中,我正在下载一个视频文件。我想显示下载的进度条。

通过 AsyncTask 概念怎么可能呢?

谢谢,

尼基

In my application i'm downloading a video file. I want to show the progress bar for the download.

How is it possible through AsyncTask Concept?

Thanks,

Niki

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

℡寂寞咖啡 2024-10-28 16:27:32

使用AsyncTask的onProgressUpdate方法。

如果您知道文件的大小,您可以在 onPreExecute 中设置最大值:

protected void onPreExecute() {
  ProgressDialog myDialog = new ProgressDialog(context);
  myDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  myDialog.setMax(maxValue);
  myDialog.show();
}

编辑:添加 myDialog.show();

有几种方法可以更新进度,可以通过增加一定量或将进度设置为特定值:

@Override
protected void onProgressUpdate(Integer... progress) {
  myDialog.incrementProgressBy(progress[0]);

  // or
  // myDialog.setProgress(progress[0]);

}

然后在 onDoInBackground():

@Override
protected void doInBackGround() {
  // your code
  publishProgress(1);
}

编辑布局中带有进度条的示例:

在布局文件中,添加一个像这样的进度条

<ProgressBar
    android:id="@+id/progressbar"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    style="?android:attr/progressBarStyleHorizontal"
    android:visibility="gone"
    />

并且在你的异步任务中:

protected void onPreExecute() {
  ProgressBar myProgress = findViewById(R.id.progressbar);
  myProgress.setMax(maxValue);
  myProgress.setVisibility(View.VISIBLE);
}

protected void onProgressUpdate(Integer... progress) {
  myProgress.setProgress(progress[0]);
}

Use the onProgressUpdate method of AsyncTask.

If you know the size of the file you can set the max value in onPreExecute:

protected void onPreExecute() {
  ProgressDialog myDialog = new ProgressDialog(context);
  myDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  myDialog.setMax(maxValue);
  myDialog.show();
}

EDIT: added myDialog.show();

There are several methods to update the progress, either by incrementing by an amount or by setting the progress to a specific value:

@Override
protected void onProgressUpdate(Integer... progress) {
  myDialog.incrementProgressBy(progress[0]);

  // or
  // myDialog.setProgress(progress[0]);

}

Then in the onDoInBackground():

@Override
protected void doInBackGround() {
  // your code
  publishProgress(1);
}

EDIT example with progressbar in layout:

In your layout file, add a progressbar like this

<ProgressBar
    android:id="@+id/progressbar"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    style="?android:attr/progressBarStyleHorizontal"
    android:visibility="gone"
    />

And in your asynctask:

protected void onPreExecute() {
  ProgressBar myProgress = findViewById(R.id.progressbar);
  myProgress.setMax(maxValue);
  myProgress.setVisibility(View.VISIBLE);
}

protected void onProgressUpdate(Integer... progress) {
  myProgress.setProgress(progress[0]);
}
月竹挽风 2024-10-28 16:27:32

首先你必须计算下载时间,然后根据时间限制实现进度条。这里我附上了进度条的示例程序

new Thread(new Runnable() {
         public void run() {
             while (mProgressStatus < 100) {
                 mProgressStatus = doWork();

                 // Update the progress bar
                 mHandler.post(new Runnable() {
                     public void run() {
                         mProgress.setProgress(mProgressStatus);
                     }
                 });

这是用于计算下载文件的时间长度的代码

//once the connection has been opened              

List value = urlConnection.getHeaderFields().get("content-Length")
if (values != null && !values.isEmpty()) {

// getHeaderFields() returns a Map with key=(String) header 
// name, value = List of String values for that header field. 
// just use the first value here.
String sLength = (String) values.get(0);

if (sLength != null) {
   //parse the length into an integer...
   }

First you have to calculate downloading timi, and then implement progress bar as per time limit. here i enclosed the sample program of progress bar

new Thread(new Runnable() {
         public void run() {
             while (mProgressStatus < 100) {
                 mProgressStatus = doWork();

                 // Update the progress bar
                 mHandler.post(new Runnable() {
                     public void run() {
                         mProgress.setProgress(mProgressStatus);
                     }
                 });

And this is the code for calculating the Time length for downloading file

//once the connection has been opened              

List values = urlConnection.getHeaderFields().get("content-Length")
if (values != null && !values.isEmpty()) {

// getHeaderFields() returns a Map with key=(String) header 
// name, value = List of String values for that header field. 
// just use the first value here.
String sLength = (String) values.get(0);

if (sLength != null) {
   //parse the length into an integer...
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文