之前在Android(java)学习笔记215中,我们从JavaSE的角度去实现了多线程断点下载,下面从Android角度实现这个断点下载:

1. 新建一个Android工程:

(1)其中我们先实现布局文件activity_main.xml:

 <LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" > <EditText
android:id="@+id/et_path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="http://192.168.1.100:8080/" /> <EditText
android:inputType="number"
android:id="@+id/et_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请设置下载线程的数量,默认3" /> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="download"
android:text="下载" /> <LinearLayout
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:id="@+id/ll_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout> </LinearLayout>

布局效果如下:

(2)其中的进度条样式pb.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_height="wrap_content" />

2. MainActivity.java:

 package com.itheima.mutiledownloader;

 import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL; import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast; public class MainActivity extends Activity {
private EditText et_path;
private EditText et_count;
private LinearLayout ll_container;
private int threadCount = 3;
private String path;
protected int runningThreadCount; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_count = (EditText) findViewById(R.id.et_count);
et_path = (EditText) findViewById(R.id.et_path);
ll_container = (LinearLayout) findViewById(R.id.ll_container);
} public void download(View view){
String str_count = et_count.getText().toString().trim();
path = et_path.getText().toString().trim();
if(TextUtils.isEmpty(path)&&!path.startsWith("http://")){
Toast.makeText(this, "下载路径不合法", 0).show();
return;
}
if(!TextUtils.isEmpty(str_count)){
threadCount = Integer.parseInt(str_count);
}
ll_container.removeAllViews();
for(int i=0;i<threadCount;i++){
ProgressBar pb = (ProgressBar) View.inflate(this, R.layout.pb,null);
ll_container.addView(pb);
}
new Thread(){
public void run() {
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
if(code == 200){
int length = conn.getContentLength();
System.out.println("服务器文件的大小为:"+length);
//创建一个空白文件文件的大小和服务器资源一样
RandomAccessFile raf = new RandomAccessFile(Environment.getExternalStorageDirectory().getPath()+"/"+getFileName(path), "rw");
raf.setLength(length);
raf.close();
//每个线程下载的平均区块大小
int blocksize = length / threadCount;
runningThreadCount = threadCount;
for(int threadId = 0;threadId<threadCount;threadId++){
int startIndex = threadId*blocksize;
int endIndex = (threadId+1)*blocksize-1;
if(threadId==(threadCount-1)){//最后一个线程的修正
endIndex = length - 1;
}
new DownloadThread(startIndex, endIndex, threadId).start();
}
}
} catch (Exception e) {
e.printStackTrace();
showToast("下载失败");
} };
}.start(); }
class DownloadThread extends Thread{
/**
* 理论上第一次开始的位置
*/
int firstStartIndex;
int startIndex;
int endIndex;
/**
* 当前线程下载到文件的位置
*/
int filePosition;
int threadId;
/**
* 当前线程需要下载的总文件大小
*/
int threadTotal;
ProgressBar pb;//当前线程对应的进度条
/**
*
* @param startIndex 开始位置
* @param endIndex 结束位置
* @param threadId 线程id
*/
public DownloadThread(int startIndex, int endIndex, int threadId) {
this.startIndex = startIndex;
this.firstStartIndex = startIndex;
this.endIndex = endIndex;
threadTotal = endIndex - startIndex;
this.threadId = threadId;
pb = (ProgressBar) ll_container.getChildAt(threadId);
pb.setMax(threadTotal);
filePosition = startIndex;
} @Override
public void run() {
//System.out.println("线程:"+threadId+"理论下载的位置:"+startIndex+"~~~"+endIndex);
//读取,看看有没有下载的历史数据,读下载的进度
try {
File file = new File(Environment.getExternalStorageDirectory().getPath()+"/"+threadId+getFileName(path)+".txt");//用一个文本记录当前线程下载的进程
if(file.exists()&&file.length()>0){
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
filePosition = Integer.parseInt(br.readLine());//上一次下载到文件的哪个位子。
startIndex = filePosition;
fis.close();
}
System.out.println("线程:"+threadId+"实际上下载的位置:"+startIndex+"~~~"+endIndex);
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Range", "bytes="+startIndex+"-"+endIndex);
int code = conn.getResponseCode();//2XX 成功 3XX重定向 4XX资源找不到 5XX服务器异常
if(code == 206){
InputStream is = conn.getInputStream();
RandomAccessFile raf = new RandomAccessFile(Environment.getExternalStorageDirectory().getPath()+"/"+getFileName(path), "rwd");
//一定要记得定位文件写的位置
raf.seek(startIndex);
byte[] buffer = new byte[1024*4];//缓冲区的大小
int len = -1;
while((len = is.read(buffer))!=-1){
raf.write(buffer, 0, len);
filePosition+=len;//记录写入数据的进度
int process = filePosition - firstStartIndex;
pb.setProgress(process);
RandomAccessFile rafinfo = new RandomAccessFile(file, "rwd");
rafinfo.write(String.valueOf(filePosition).getBytes());
rafinfo.close();
}
raf.close();
is.close();
System.out.println("线程:"+threadId+"下载完毕了。");
synchronized (MainActivity.this) {//加锁,同步运行,保证下面一段代码在同一个时间片运行,原子性执行
runningThreadCount--;
if(runningThreadCount==0){
System.out.println("所有的线程都下载完毕了");
showToast("下载完毕了");
for(int i =0;i<threadCount;i++){
File f = new File(Environment.getExternalStorageDirectory().getPath()+"/"+i+getFileName(path)+".txt");
System.out.println(f.delete());
}
}
} }
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 获取路径对应的文件名
* @param path
* @return
*/
private static String getFileName(String path){
int beginIndex = path.lastIndexOf("/")+1;
return path.substring(beginIndex);
} private void showToast(final String text){
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, text, 0).show();
}
});
}
}

3. 其中AndroidMainfest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.mutiledownloader"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.mutiledownloader.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>

 

Android(java)学习笔记216:多线程断点下载的原理(Android实现)的更多相关文章

  1. 我的Android进阶之旅------>Android基于HTTP协议的多线程断点下载器的实现

    一.首先写这篇文章之前,要了解实现该Android多线程断点下载器的几个知识点 1.多线程下载的原理,如下图所示 注意:由于Android移动设备和PC机的处理器还是不能相比,所以开辟的子线程建议不要 ...

  2. Android(java)学习笔记159:多线程断点下载的原理(Android实现)

    之前在Android(java)学习笔记215中,我们从JavaSE的角度去实现了多线程断点下载,下面从Android角度实现这个断点下载: 1. 新建一个Android工程: (1)其中我们先实现布 ...

  3. Android(java)学习笔记215:多线程断点下载的原理(JavaSE实现)

    1. 为什么需要多线程下载?     服务器的资源有限,同时的平均地分配给每个客户端.开启的线程越多抢占的服务的资源就越多,下载的速度就越块. 2. 下载速度的限制条件? (1)你的电脑手机宽带的带宽 ...

  4. Android(java)学习笔记158:多线程断点下载的原理(JavaSE实现)

    1. 为什么需要多线程下载?     服务器的资源有限,同时的平均地分配给每个客户端.开启的线程越多抢占的服务的资源就越多,下载的速度就越块. 2. 下载速度的限制条件? (1)你的电脑手机宽带的带宽 ...

  5. 【原】Java学习笔记032 - 多线程

    package cn.temptation; public class Sample01 { public static void main(String[] args) { /* * [进程]:正在 ...

  6. Java学习笔记之——多线程

    多线程编程 程序: 进程:一个程序运行就会产生一个进程 线程:进程的执行流程,一个进程至少有一个线程,称为主线程 如:QQ聊着天,同时在听音乐 一个进程可以有多个线程,多个线程共享同一个进程的资源 线 ...

  7. Java学习笔记:多线程(一)

    Java中线程的五种状态: 新建状态(New) 就绪状态(Runnable) 运行状态(Running) 阻塞状态(Blocked) 凋亡状态(Dead) 其中阻塞状态(Blocked)又分为三种: ...

  8. java学习笔记(5)多线程

    一.简介(过段时间再写,多线程难度有点大) --------------------------------------- 1.进程:运行时的概念,运行的应用程序 2.线程:应用程序内部并发执行的代码 ...

  9. Java 学习笔记(11)——多线程

    Java内部提供了针对多线程的支持,线程是CPU执行的最小单位,在多核CPU中使用多线程,能够做到多个任务并行执行,提高效率. 使用多线程的方法 创建Thread类的子类,并重写run方法,在需要启动 ...

随机推荐

  1. CSS响应式web设计

    参考 1. 响应式web设计之CSS3 Media Queries http://www.cnblogs.com/mofish/archive/2012/05/23/2515218.html 2. 用 ...

  2. TatukGIS - GisDefs - ChangeDir 函数

    函数名称  ChangeDir 所在单元  GisDefs 函数原型  function ChangeDir(const _path: String): String;   函数说明 如果 _path ...

  3. 七天学会SALTSTACK自动化运维 (2)

    七天学会SALTSTACK自动化运维 (2) 导读 Grains Pillar 总结 参考链接 导读 上一篇主要介绍了安装和基本的使用方法,但是我认为如果理解了相关概念的话,使用会更加顺手,因为毕竟每 ...

  4. S5PV210开发板刷机(SD卡uboot、串口+USB-OTG刷机方法)

    一.介绍 九鼎的S5PV210开发板,在出厂前已经默认刷了Android4.0系统.如果需要刷其它的系统或者是由于系统问题无法启动时,就需要对板子刷机. 其实,刷机是对210开发板的一个基础学习,目的 ...

  5. shell排序算法

    今天看<The C Programming Language>的时候看到了shell排序算法, /* shellsort: sort v[0]...v[n-1] into increasi ...

  6. sql 使用 FOR XML PATH实现字符串拼接

    sql中经常需要把多个行数据合成一行下面是利用 FOR XML PATH来实现的简单介绍. 1,把图一的转换为图二: SELECT articleID, (),tagID)+',' FROM arti ...

  7. iOS类别(Category)与扩展(Extension)-b

    苹果的官方文档 Category在iOS开发中使用非常频繁.尤其是在为系统类进行拓展的时候,我们可以不用继承系统类,直接给系统类添加方法,最大程度的体现了Objective-C的动态语言特性. #im ...

  8. Spring中用@Component、@Repository、@Service和 @Controller等标注的默认Bean名称会是小写开头的非限定类名

    今天用调度平台去调用bean中的方法时,提示找不到bean.经查,发现是由于如果在标注上没有提供name属性值,则默认的bean名称是小写开头的,而不是大写开头的. 下面是其他文档参阅: 使用过滤器自 ...

  9. Delphi控件的透明与不透明(要挨个解释一下原因),对InvalidateControl的关键理解

    procedure TForm1.Button3Click(Sender: TObject);begin if (csOpaque in ControlStyle) then ShowMessage( ...

  10. 微信下载APK

    腾讯应用宝 微信屏蔽了来源不是腾讯的APK function isWeixin() { var ua = navigator.userAgent.toLowerCase() if(ua.match(/ ...