栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

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

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

途中碰到的问题: 

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

2、子线程不能修改 UI 

3、允许网络协议 

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

fragment_main 布局文件 



  
  
  
  

  

strings.xml 




  hwdownload
  Hello world!
  Settings
  开始
  暂停
  继续



(问题3)在 AndroidManifest 文件中配置
 
   

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;


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资源.....***************

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/161557.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号