Android开发之多线程下载、断点续传、进度条和文本显示
代码实现了在Android环境下的多线程下载、断点续传、进度条显示和文本显示百分数:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL; import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView; public class MainActivity extends Activity { static int ThreadCount = 3;
static int finishedThread = 0; int currentProgress;
String fileName = "QQPlayer.exe";
//确定下载地址
String path = "http://192.168.13.13:8080/" + fileName;
private ProgressBar pb;
TextView tv; Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
//把变量改成long,在long下运算
tv.setText((long)pb.getProgress() * 100 / pb.getMax() + "%");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb = (ProgressBar) findViewById(R.id.pb);
tv = (TextView) findViewById(R.id.tv);
} public void click(View v){ Thread t = new Thread(){
@Override
public void run() {
//发送get请求,请求这个地址的资源
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000); if(conn.getResponseCode() == 200){
//拿到所请求资源文件的长度
int length = conn.getContentLength(); //设置进度条的最大值就是原文件的总长度
pb.setMax(length); File file = new File(Environment.getExternalStorageDirectory(), fileName);
//生成临时文件
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
//设置临时文件的大小
raf.setLength(length);
raf.close();
//计算出每个线程应该下载多少字节
int size = length / ThreadCount; for (int i = 0; i < ThreadCount; i++) {
//计算线程下载的开始位置和结束位置
int startIndex = i * size;
int endIndex = (i + 1) * size - 1;
//如果是最后一个线程,那么结束位置写死
if(i == ThreadCount - 1){
endIndex = length - 1;
}
// System.out.println("线程" + i + "的下载区间是:" + startIndex + "---" + endIndex);
new DownLoadThread(startIndex, endIndex, i).start();
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
t.start();
} class DownLoadThread extends Thread{
int startIndex;
int endIndex;
int threadId; public DownLoadThread(int startIndex, int endIndex, int threadId) {
super();
this.startIndex = startIndex;
this.endIndex = endIndex;
this.threadId = threadId;
} @Override
public void run() {
//再次发送http请求,下载原文件
try {
File progressFile = new File(Environment.getExternalStorageDirectory(), threadId + ".txt");
//判断进度临时文件是否存在
if(progressFile.exists()){
FileInputStream fis = new FileInputStream(progressFile);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
//从进度临时文件中读取出上一次下载的总进度,然后与原本的开始位置相加,得到新的开始位置
int lastProgress = Integer.parseInt(br.readLine());
startIndex += lastProgress; //把上次下载的进度显示至进度条
currentProgress += lastProgress;
pb.setProgress(currentProgress); //发送消息,让主线程刷新文本进度
handler.sendEmptyMessage(1);
fis.close();
}
System.out.println("线程" + threadId + "的下载区间是:" + startIndex + "---" + endIndex);
HttpURLConnection conn;
URL url = new URL(path);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
//设置本次http请求所请求的数据的区间
conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex); //请求部分数据,相应码是206
if(conn.getResponseCode() == 206){
//流里此时只有1/3原文件的数据
InputStream is = conn.getInputStream();
byte[] b = new byte[1024];
int len = 0;
int total = 0;
//拿到临时文件的输出流
File file = new File(Environment.getExternalStorageDirectory(), fileName);
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
//把文件的写入位置移动至startIndex
raf.seek(startIndex);
while((len = is.read(b)) != -1){
//每次读取流里数据之后,同步把数据写入临时文件
raf.write(b, 0, len);
total += len;
System.out.println("线程" + threadId + "下载了" + total); //每次读取流里数据之后,把本次读取的数据的长度显示至进度条
currentProgress += len;
pb.setProgress(currentProgress);
//发送消息,让主线程刷新文本进度
handler.sendEmptyMessage(1); //生成一个专门用来记录下载进度的临时文件
RandomAccessFile progressRaf = new RandomAccessFile(progressFile, "rwd");
//每次读取流里数据之后,同步把当前线程下载的总进度写入进度临时文件中
progressRaf.write((total + "").getBytes());
progressRaf.close();
}
System.out.println("线程" + threadId + "下载完毕-------------------小志参上!");
raf.close(); finishedThread++;
synchronized (path) {
if(finishedThread == ThreadCount){
for (int i = 0; i < ThreadCount; i++) {
File f = new File(Environment.getExternalStorageDirectory(), i + ".txt");
f.delete();
}
finishedThread = 0;
}
} }
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }
Android开发之多线程下载、断点续传、进度条和文本显示的更多相关文章
- Android开发-各种各样好看漂亮的进度条,指示器,加载提示汇总
导读:之前项目中用到一些进度条,找了不少,打算写个demo自己总结一下,留着以后用, 有些是自己写的,有些是github上找的别人的库,如果大家觉得好看可以用,直接下载复制代码到项目里就可以用,ok ...
- 赵雅智_android多线程下载带进度条
progressBar说明 在某些操作的进度中的可视指示器,为用户呈现操作的进度,还它有一个次要的进度条,用来显示中间进度,如在流媒体播放的缓冲区的进度. 一个进度条也可不确定其进度.在不确定模式下, ...
- android 开发 制作弹出等待进度条
技术点: dialog:ProgressBar:animated-rotate: 弹出框: import com.carspeak.client.R; import android.app.Dialo ...
- iOS之UI--Quartz2D的入门应用--重绘下载圆形进度条
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- android 自定义控件——(四)圆形进度条
----------------------------------↓↓圆形进度条(源代码下有属性解释)↓↓---------------------------------------------- ...
- 使用 new XMLHttpRequest() 制作下载文件进度条
mui 进度控件使用方法: 检查当前容器(container控件)自身是否包含.mui-progressbar类: 当前容器包含.mui-progressbar类,则以当前容器为目标控件,直接显示进度 ...
- android 网络异步加载数据进度条
ProgressDialog progressDialog = null; public static final int MESSAGETYPE = 0; private void execute( ...
- canvas绘制圆形进度条(或显示当前已浏览网页百分比)
使用canvas绘制圆形进度条,或者是网页加载进度条 或者是显示你浏览了本网页多少-- 由于个浏览器的计算差异,打开浏览器时 初始值有所不同,但是当拉倒网页底部时,均显示100%. 兼容性:测试浏览器 ...
- Android使用OKHttp3实现下载(断点续传、显示运行进度)
OKHttp3是现在很流行的Android网络请求框架,那么怎样利用Android实现断点续传呢,今天写了个Demo尝试了一下,感觉还是有点意思 准备阶段 我们会用到OKHttp3来做网络请求,使用R ...
随机推荐
- hadoop 文件 复制 移动 FileUtil.copy
FileSystem fs = FileSystem.get(conf); Path pattern = new Path(resultViewPath + "phone*"); ...
- Openvpn完美解决公司网络没有固定公网IP的问题
方案背景: 公司办公网络使用长城宽带上网有一段时间了,有4个固定IP(2个电信,2个网通),链路不太稳定,经常有问题,因此考虑取消长城宽带,采用原来的adsl上网.但是有个问题,因为公司内网有几台服务 ...
- 字符设备驱动中cdev与inode、file_operations的关系
一.cdev与inode 二.cdev与file_operations
- 安装Nuget上常用的包的命令
起因: Nuget图形化操作界面各种卡顿,或者有时干脆就连不上了.所以用命令还是很必须的. 常用命令: 安装 Entity Framework : PM> Install-Package Ent ...
- 【BZOJ 2440】[中山市选2011]完全平方数
Description 小 X 自幼就很喜欢数.但奇怪的是,他十分讨厌完全平方数.他觉得这些数看起来很令人难受.由此,他也讨厌所有是完全平方数的正整数倍的数.然而这丝毫不影响他对其他数的热爱. 这天是 ...
- how to get sharepoint lookup value
SPFieldLookup lookUp1 = properties.ListItem.ParentList.Fields.GetField("Leave_x0020_Type") ...
- pageControl指示器和图片放大-b
小编由于篇幅问题,截取了最后一篇,如果需要看其他的三篇文章,可以去笔者的简书看:http://www.jianshu.com/users/9f3739421d15/latest_articles 另外 ...
- centos7 下载eclipse的镜像站点
这里吐槽一下,由于两天前centos被我农崩溃了(系统更新的锅),所以所有的开发环境又得重来一次. 其实,之前去eclipse的官网下载就很慢,打开官网也很慢,然后你会发现下下来的安装程序(只有40多 ...
- PHP5 session 详解
http协议是WEB服务器与客户端(浏览器)相互通信的协议,它是一种无状态协议.所谓无状态,指的是不会维护http请求数据,http请求是独立的,非持久的.而越来越复杂的WEB应用,需要保存一些用户状 ...
- Ehcache 整合Spring 使用页面、对象缓存(转载)
Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...