Android之ProgressBar即时显示下载进度详解

 更新时间:2016年09月02日 14:58:49   作者:西门吃雪  
这篇文章主要为大家详细介绍了Android之ProgressBar即时显示下载进度,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

这里利用 ProgressBar 即时显示下载进度。 

途中碰到的问题: 

1、主线程中不能打开 URL,和只能在主线程中使用 Toast 等 

2、子线程不能修改 UI 

3、允许网络协议 

4、暂停下载和继续下载
   ........ 

fragment_main 布局文件 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.dragon.android.textbar.MainActivity$PlaceholderFragment" >

  <!-- prigressBar 进度条 -->
  <!-- progress 当前进度 -->
  <!-- indeterminate 不明确的  默认false -->
  <ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:max="100"
    android:progress="0"
    android:indeterminate="true"/>

  <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:onClick="startLoad"
    android:layout_marginTop="86dp"
    android:background="#009FEE"
    android:text="@string/start"
    android:textColor="#ffffff" />

  <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/progressBar1"
    android:background="@null"
    android:layout_alignParentLeft="true" />
  
</RelativeLayout>

strings.xml 

<?xml version="1.0" encoding="utf-8"?>
<resources>

  <string name="app_name">hwdownload</string>
  <string name="hello_world">Hello world!</string>
  <string name="action_settings">Settings</string>
  <string name="start">开始</string>
  <string name="stop">暂停</string>
  <string name="contin">继续</string>

</resources>

(问题3)在 AndroidManifest 文件中配置
 <!-- 请求网络权限 -->
    <uses-permission  android:name="android.permission.INTERNET"/>

MainActivity(问题1、2) 

package com.dragon.android.textbar;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 只有创建一个 View 的线程才可以改变这个 View 的UI!!! 主线程也叫 UI 线程
 */
public class MainActivity extends Activity {
  private ProgressBar progressBar1;
  private Button button1;
  private TextView textView1;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_main);

    progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
    button1 = (Button) findViewById(R.id.button1);
    textView1 = (TextView) findViewById(R.id.textView1);

  }

  public void startLoad(View view) {
     String text = (String) button1.getText();
    // 设置按钮内容 ----并没有用...
    button1.setText(text.equals(getResources().getString(R.string.start)) ? R.string.stop
        : (text.equals(getResources().getString(R.string.stop)) ? R.string.contin
            : R.string.stop));
    progressBar1.setIndeterminate(false);

    new Thread(new Runnable() {
      private int percent;

      @Override
      public void run() {
        try {
          // 打开 URL 必须在子线程
          URL url = new URL(
              "http://b.zol-img.com.cn/sjbizhi/images/9/540x960/1472549276394.jpg");
          HttpURLConnection conn = (HttpURLConnection) url.openConnection();
          // conn.setRequestMethod("GET");
          // conn.setReadTimeout(5000);
          // conn.setConnectTimeout(5000);

          int contentLength = conn.getContentLength();

          if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            InputStream is = conn.getInputStream();

            byte[] buffer = new byte[1024];
            int len = -1;
            int sum = 0;
            while ((len = is.read(buffer)) != -1) {
              sum += len;
              // 注意强转方式,防止一直为0
              percent = (int) (100.0 * sum / contentLength);
              // 在主线程上运行的子线程
              runOnUiThread(new Runnable() {

                @Override
                public void run() {
                  progressBar1.setProgress(percent);
                  textView1.setText(percent + "%");
                  if (percent == progressBar1.getMax()) {
                    Toast.makeText(MainActivity.this,
                        "下载完成!", Toast.LENGTH_SHORT)
                        .show();
                  }
                }
              });
            }
            is.close();
            conn.disconnect();
          }
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }).start();
  }
}

**************然而并没有解决问题4,要用断点续传,但是还不会存放assets资源.....***************

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android添加联系人到通讯录的方法

    Android添加联系人到通讯录的方法

    本周项目中遇到了需要添加联系人或者添加到已有联系人的需求,联系人中需要保存的字段有很多,之前不太熟悉,在这里总结一下。
    2021-05-05
  • Android使用AndroidUtilCode实现多语言

    Android使用AndroidUtilCode实现多语言

    这篇文章主要为大家介绍了Android使用AndroidUtilCode实现多语言示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • android自定义ImageView仿图片上传示例

    android自定义ImageView仿图片上传示例

    本篇文章主要介绍了android自定义ImageView仿图片上传,具有一定的参考价值,有兴趣的可以了解一下。
    2017-01-01
  • Android延迟实现的几种解决方法及原理分析

    Android延迟实现的几种解决方法及原理分析

    这篇文章主要给大家介绍了关于Android延迟实现的几种解决方法以及其中的原理分析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-12-12
  • Android实现帧动画的两种方式

    Android实现帧动画的两种方式

    帧动画(Frame Animation)是一种在一定时间内按顺序播放一系列图像帧(每一帧都是一个单独的图像),从而产生连续运动或变化的动画效果,本文给大家介绍了Android实现帧动画的两种方式,需要的朋友可以参考下
    2024-02-02
  • Android ViewPager自定义轮播图并解决播放冲突

    Android ViewPager自定义轮播图并解决播放冲突

    这篇文章主要为大家详细介绍了Android ViewPager自定义轮播图并解决播放冲突,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • Android Studio ADB网络调试汇总

    Android Studio ADB网络调试汇总

    这篇文章主要为大家详细介绍了Android Studio ADB网络调试的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • 深入分析Android加载so文件源码

    深入分析Android加载so文件源码

    这篇文章主要介绍了深入分析Android加载so文件源码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • Android实现进度条(ProgressBar)的功能与用法

    Android实现进度条(ProgressBar)的功能与用法

    这篇文章主要为大家详细介绍了Android实现进度条(ProgressBar)的功能与用法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-08-08
  • 自定义视图view之环形进度条

    自定义视图view之环形进度条

    这篇文章主要介绍了自定义视图view之环形进度条,这次介绍了4种不同的效果,直接上代码了,需要的朋友可以参考下
    2023-04-04

最新评论