android 断点下载---XUtils
配置权限
1 2 | <uses-permission android:name= "android.permission.INTERNET" /> <uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" /> |
/MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | package com.bawei.myxutils; import java.io.File; import java.math.BigDecimal; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; 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; public class MainActivity extends Activity { private ProgressBar pb; private TextView text1; private TextView text2; private String path; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pb = (ProgressBar) findViewById(R.id.progressBar1); // 进度条 text1 = (TextView) findViewById(R.id.text1); // 显示当前下载了多少 text2 = (TextView) findViewById(R.id.text2); // 显示下载总大小 path = Environment.getExternalStorageDirectory().getPath(); // 得到SD卡路径 HttpUtils http = new HttpUtils(); /** * url 下载的路径 target 下载文件保存的路径 autoResume 是否支持断点续传 */ HttpHandler handler = http.download( "http://101.200.142.201:8080/tqyb/dancer.avi" , path + "/dancer.avi" , true /* 如果目标文件存在,接着未完成的部分继续下载。 */ , true /* 如果从请求返回信息中获取到文件名,下载完成后自动重命名。 */ , new RequestCallBack<File>() { @Override public void onStart() { // TODO Auto-generated method stub super.onStart(); Toast.makeText(MainActivity. this , "开始下载" , 0).show(); } /** * 加载进度 pb为进度条,实现进度条进度加载 total 总得大小 current 当前的大小 */ @Override public void onLoading( long total, long current, boolean isUploading) { // TODO Auto-generated method stub pb.setMax(( int ) total); pb.setProgress(( int ) current); // 将字节转换成M BigDecimal filesize = new BigDecimal(current); BigDecimal megabyte = new BigDecimal(1024 * 1024); float returnValue = filesize.divide(megabyte, 2, BigDecimal.ROUND_UP).floatValue(); text1.setText(returnValue + "M" ); // 将字节转换成M BigDecimal filesize1 = new BigDecimal(total); BigDecimal megabyte1 = new BigDecimal(1024 * 1024); float returnValue1 = filesize1.divide(megabyte1, 2, BigDecimal.ROUND_UP).floatValue(); text2.setText(returnValue1 + "M" ); } @Override public void onSuccess(ResponseInfo<File> arg0) { // TODO Auto-generated method stub Toast.makeText(MainActivity. this , "下载成功" , 0).show(); } @Override public void onFailure(HttpException arg0, String arg1) { // TODO Auto-generated method stub Toast.makeText(MainActivity. this , "下载失败" , 0).show(); } }); } } |
效果展示
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 探秘 MySQL 索引底层原理,解锁数据库优化的关键密码(下)
· 大模型 Token 究竟是啥:图解大模型Token
· 35岁程序员的中年求职记:四次碰壁后的深度反思
· 继承的思维:从思维模式到架构设计的深度解析
· 如何在 .NET 中 使用 ANTLR4
· 2025,回顾出走的 10 年
· 【保姆级教程】windows 安装 docker 全流程
· 分享 3 款基于 .NET 开源且免费的远程桌面工具
· 基于Docker+DeepSeek+Dify :搭建企业级本地私有化知识库超详细教程
· 由 MCP 官方推出的 C# SDK,使 .NET 应用程序、服务和库能够快速实现与 MCP 客户端