Android 使用开源xUtils来实现多线程下载(非原创)
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器