1.程序员自己也是可以实现多线程下载的,只是代码量比较大,而且,其中有许多细节需要考虑到,在GitHub上有人写好的代码,我们可以拿过来使用下,节省了我们开发程序的时间

2.导包:xUtils-2.6.14.jar,文件可以去https://github.com/wyouflf/xUtils下载

3.fragment_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" > <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="开始下载"
android:onClick="click" /> <ProgressBar
android:id="@+id/pb_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Horizontal"
/>
<TextView
android:id="@+id/tv_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#A52A2A"
android:textSize="25sp"
/>
<TextView
android:id="@+id/tv_failure"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FF6347" /> </LinearLayout>

4.MainActivity.java

package com.example.xutilsmultithreaddownload;

import java.io.File;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack; import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build; public class MainActivity extends Activity {
//错误提示的文本
private TextView tv_failure;
//进度条
private ProgressBar pb_bar;
//进度显示的文本
private TextView tv_progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
tv_failure = (TextView) findViewById(R.id.tv_failure);
pb_bar = (ProgressBar) findViewById(R.id.pb_bar);
tv_progress = (TextView) findViewById(R.id.tv_progress);
} public void click(View v) {
// 文件名字
String fileName = "ESurfing_V2.1.exe";
// 路径
String path = "http://192.168.1.66:8080/" + fileName;
HttpUtils http = new HttpUtils();
HttpHandler handler = http.download(path, Environment.getExternalStorageDirectory() + "/" + fileName, true, true, new RequestCallBack<File>() {
//下载成功后调用
@Override
public void onSuccess(ResponseInfo<File> arg0) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, arg0.result.getPath(), 0).show();
}
//下载失败调用
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
tv_failure.setText(arg1);
}
//下载的时候调用
@Override
public void onLoading(long total, long current, boolean isUploading) {
// TODO Auto-generated method stub
super.onLoading(total, current, isUploading);
//设置进度条的最大值
pb_bar.setMax((int)total);
//设置进度当前的进度
pb_bar.setProgress((int)current);
//文本显示当前的进度
tv_progress.setText(current*100/total+"%");
}
});
}
}

5.加入权限

 <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

6.xUtils使用

7.运行效果

Android 使用开源xUtils来实现多线程下载(非原创)的更多相关文章

  1. 网络编程之PC版与Android手机版带断点续传的多线程下载

    一.多线程下载         多线程下载就是抢占服务器资源         原理:服务器CPU 分配给每条线程的时间片相同,服务器带宽平均分配给每条线程,所以客户端开启的线程越多,就能抢占到更多的服 ...

  2. Android开发(24)---安卓中实现多线程下载(带进度条和百分比)

    当我们学完java中多线程的下载后,可以将它移植到我们的安卓中来,下面是具体实现源码: DownActivity.java package com.example.downloads; import ...

  3. android 中获取视频文件的缩略图(非原创)

    在android中获取视频文件的缩略图有三种方法: 1.从媒体库中查询 2. android 2.2以后使用ThumbnailUtils类获取 3.调用jni文件,实现MediaMetadataRet ...

  4. android 学习随笔十三(网络:多线程下载)

    多线程断点续传下载1.多线程:快* 原理:抢占服务器资源* 单线程下载:线程从第0个字节开始下,下到最后一个字节,在本地硬盘的临时文件中从第0个字节开始写,写到最后一个字节,下载完成时,临时文件也写完 ...

  5. *Android 多线程下载 仿下载助手(改进版)

    首先声明一点: 这里的多线程下载 并非指的 多个线程下载一个 文件.而是 每一个线程 负责一个文件. 真正的多线程 希望后面能给大家带来.  -------------  欢迎 爱学习的小伙伴 加群 ...

  6. Andoid 更好的Android多线程下载框架

    概述 为什么是更好的Android多线程下载框架呢,原因你懂的,广告法嘛! 本篇我们我们就来聊聊多线程下载框架,先聊聊我们框架的特点: 多线程 多任务 断点续传 支持大文件 可以自定义下载数据库 高度 ...

  7. 更好的Android多线程下载框架

    /** * 作者:Pich * 原文链接:http://me.woblog.cn/ * QQ群:129961195 * Github:https://github.com/lifengsofts */ ...

  8. Android 手机卫士--xutils说明与下载方法使用

    xUtils简介 xUtils 包含了很多实用的android工具. xUtils 最初源于Afinal框架,进行了大量重构,使得xUtils支持大文件上传,更全面的http请求协议支持(10种谓词) ...

  9. android 多线程下载 断点续传

    来源:网易云课堂Android极客班第八次作业练习 练习内容: 多线程 asyncTask handler 多线程下载的原理 首先获取到目标文件的大小,然后在磁盘上申请一块空间用于保存目标文件,接着把 ...

随机推荐

  1. html5新特性--音频视频,拖放

    1.音频 <audio controls> <source src="aaa.ogg" type="audio/ogg"> <so ...

  2. JS文件中加载jquery.js

    原文链接:http://blog.csdn.net/whatday/article/details/39553451 最近有一个需求: 1.在一个html中只能引入一个JS文件 不能有JS代码和其他J ...

  3. WPF bitmap转bitmapimage 使用 CreateBitmapSourceFromHBitmap内存泄漏

    IntPtr f = bmp.GetHbitmap(); img.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitm ...

  4. 使用ganymed-ssh2-build通过ssh获得远程服务器参数

    1.项目中需要检测到几台远程服务器的参数,差了很多资料,决定用的这个 2.jar包:ganymed-ssh2-build210.jar 3.原理:向远程linux服务器发送脚本命令,得到该台服务器的信 ...

  5. PHPexcel 判断日期类型

    若已经确定某列为日期型数据: for($currentRow=2;$currentRow <= $allRow;$currentRow++){ //从哪列开始,A表示第一列 for($curre ...

  6. eclipse Content Assist 无法使用,不能自动补全的解决办法

    今天用eclipse写JAVA代码,写着写着突然,eclipse 的自动补全功能失效了,没办法自动补全.折腾半天,终于解决了. 在window->Preferences->Java-> ...

  7. form 练习

    <form>姓:<input type="text" name="firstname" /><br />名:<inpu ...

  8. 收集点小文,讲CGI,FASTCGI,PHP-CGI,PHP-FPM之间通透点的文章

    http://blog.csdn.net/meegomeego/article/details/36180343 http://www.opsers.org/linux-home/server/php ...

  9. Keil C51内存分配与优化

    C51的内存分配不同于一般的PC,内存空间有限,采用覆盖和共享技术.在Keil编译器中,经过编译后,会形成一个M51文件,在其内部可以详细的看到内存的分配情况. C51内存常见的两个误区: A.变量超 ...

  10. windows 守护进程

    use Win32::Process::Info; while (1==1){ use Sys::Hostname; use HTTP::Date qw(time2iso str2time time2 ...