工作闲余网查了一番Android多线程断点下载的资料,大多数方式都是将整个文件分段然后再分给几个线程进行下载,这里个人觉着分块下载最后整合的方式速度尚可,经过整合后一个download-manager.jar(包中有源码附有文字说明,通俗易懂),此包同时依赖http://alian008580101.iteye.com/blog/1948303中的后面四个包,下载导入项目即可。
效果图1(单击下载):
效果图2(暂停下载):
效果图3(继续下载):
效果图4(取消下载):
效果图5(重新下载):
效果图6(完成下载):
使用方法:
1.布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/buttonDownload" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/textInfo" android:layout_width="match_parent" android:layout_height="wrap_content" android:lines="2" android:gravity="center" android:textSize="18sp" android:text="wwwww"/> <com.whl.downloadmanager.TextProgressBar android:id="@+id/progressBarTest" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="20dp"/> <Button android:id="@+id/buttonCancel" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="取消下载"/> </LinearLayout>
2.自定义进度条(网上寻找的可以显示进度)
package com.whl.downloadmanager; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.util.AttributeSet; import android.widget.ProgressBar; public class TextProgressBar extends ProgressBar { private String text; private Paint mPaint; private boolean isTextVisibility = true; public TextProgressBar(Context context) { super(context); initText(); } public TextProgressBar(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initText(); } public TextProgressBar(Context context, AttributeSet attrs) { super(context, attrs); initText(); } @Override public void setProgress(int progress) { setText(progress); super.setProgress(progress); } @Override protected synchronized void onDraw(Canvas canvas) { super.onDraw(canvas); Rect rect = new Rect(); this.mPaint.getTextBounds(this.text, 0, this.text.length(), rect); int x = (getWidth() / 2) - rect.centerX(); int y = (getHeight() / 2) - rect.centerY(); canvas.drawText(this.text, x, y, this.mPaint); } // 初始化,画笔 private void initText() { this.mPaint = new Paint(); this.mPaint.setAntiAlias(true); this.mPaint.setColor(Color.WHITE); } // 设置文字内容 private void setText(int progress) { if(isTextVisibility){ float percent = (progress * 1.0f / this.getMax()) * 100; this.text = String.format("%.2f%%", percent); }else{ this.text = ""; } } public void setTextVisibility(boolean isTextVisibility){ this.isTextVisibility = isTextVisibility; } }
3.MainActivity
package com.whl.downloadmanager; import java.io.File; import android.app.Activity; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import com.whl.database.CreateDatabaseHelper; import com.whl.helper.activity.DownloadListener; import com.whl.helper.activity.MultiThreadDownload; public class MainActivity extends Activity implements OnClickListener,DownloadListener{ private static final String TAG = "MainActivity"; private static final String SD_PATH = Environment.getExternalStorageDirectory().getPath(); private boolean isPause = false; private MultiThreadDownload multiThreadDownload ; private Button buttonDownload; private Button buttonCancel; private TextProgressBar progressBarTest;//可以使用普通的进度,该进度条是可以显示现在完成度 private TextView textInfo; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);//布局文件就是两个简单的按钮一个用于暂停继续下载,一个用于取消任务,还有一个文本用于显示已完成,文件大小,下载完成度,下载速度等信息以及一个进度条。 //http://d1.apk8.com:8020/game/plantsvszombies2.apk buttonDownload = (Button)findViewById(R.id.buttonDownload); buttonDownload.setText("单击下载"); buttonDownload.setOnClickListener(this); progressBarTest = (TextProgressBar) findViewById(R.id.progressBarTest); textInfo = (TextView) findViewById(R.id.textInfo); buttonCancel = (Button) findViewById(R.id.buttonCancel); buttonCancel.setOnClickListener(this); } @Override public void onClick(View v) { String downloadUrl = "http://dldir1.qq.com/invc/qqpinyin/QQInput4.1_1133(1001).apk"; String savePath = SD_PATH+File.separator+"downloadHelper"+File.separator; String fileName = "QQInput.apk"; switch(v.getId()){ case R.id.buttonDownload: if(!isPause){ multiThreadDownload = new MultiThreadDownload(MainActivity.this,downloadUrl, savePath, fileName, this); multiThreadDownload.start(); buttonDownload.setText("下载..."); isPause = true; }else{ buttonDownload.setText("暂停..."); isPause = false; multiThreadDownload.onPause(); multiThreadDownload = null; } break; case R.id.buttonCancel: if(multiThreadDownload==null){ multiThreadDownload = new MultiThreadDownload(MainActivity.this,downloadUrl, savePath, fileName, this); } Log.d(TAG, ""+multiThreadDownload.isDownload()); if(multiThreadDownload.isDownload()){ multiThreadDownload.onCancel(); isPause = false; } multiThreadDownload = null; break; } } private Handler multiHandler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(msg.what==1){ Bundle data = msg.getData(); float downloadSpeed = data.getFloat("downloadSpeed"); boolean speedHigh = false; if(downloadSpeed>500){ speedHigh = true; downloadSpeed/=1024; } progressBarTest.setProgress(data.getInt("completedSize")); textInfo.setText(String.format("已完成:%.2fMB", data.getInt("completedSize")/1024f/1024)+"/"+ String.format("总大小:%.2fMB", data.getInt("fileSize")/1024f/1024)+"\n"+ String.format("进度:%.2f%%", data.getBoolean("isCompleted")?100.00:data.getFloat("downloadPercent"))+"/"+ String.format(speedHigh?"速度:%.2fM/S":"速度:%.2fKB/S", downloadSpeed)); if(data.getBoolean("isCompleted")){ buttonDownload.setText("下载完成"); textInfo.setText("下载完成"); } } } }; @Override public void onDownloading(boolean isCompleted,boolean isPause,boolean isCancel,int fileSize,int completedSize,int downloadedSize,float downloadPercent, float downloadSpeed) { Log.d("MainActivity", isCompleted+"||"+isPause+"||"+completedSize+"||"+downloadedSize+"||"+downloadPercent+"||"+downloadSpeed); Message message = Message.obtain(); message.what = 1; Bundle data = new Bundle(); data.putBoolean("isCancel", isCancel); data.putBoolean("isCompleted", isCompleted); data.putInt("fileSize", fileSize); data.putInt("completedSize", completedSize); data.putInt("downloadedSize", downloadedSize); data.putFloat("downloadPercent", downloadPercent); data.putFloat("downloadSpeed", downloadSpeed); message.setData(data); multiHandler.sendMessage(message); } @Override public void onBeforeDownload(boolean isCompleted,boolean isPause,boolean isCancel,int fileSize){ progressBarTest.setMax(fileSize); } @Override public void onAfterDownload(boolean isCompleted,boolean isPause,boolean isCancel,int fileSize){ } @Override public void onPause(boolean isCompelted,boolean isPause,boolean isCancel,int fileSize,int completedSize,int downloadedSize,float downloadPercent,float downloadSpeed){ textInfo.setText("暂停..."); Log.d(TAG, "暂停..."); } @Override public void onCancelDownload(){ progressBarTest.setProgress(0); textInfo.setText("下载取消"); buttonDownload.setText("重新下载"); } }
相关推荐
官方版本,亲测可用
vmware-unity-helper.exe
google-access-helper2022-main.zipgoogle-access-helper2022-main.zipgoogle-access-helper2022-main.zipgoogle-access-helper2022-main.zipgoogle-access-helper2022-main.zipgoogle-access-helper2022-main....
maven-helper-plugin-1.2.7.jar
在实际使用中,开发者需要将这些JAR文件添加到项目的类路径中,然后可以通过创建VelocityContext对象,填充数据,最后使用Velocity Engine渲染模板。这整个过程是完全独立于具体的服务器环境的,使得Velocity成为一...
NULL 博文链接:https://alian008580101.iteye.com/blog/1948303
这两个库通常与Struts1框架结合使用,以实现Web应用中的文件上传操作。 **Apache Commons FileUpload** Apache Commons FileUpload是Apache软件基金会的一个开源项目,提供了一套处理HTTP请求中多部分/表单数据的...
4. 运算符:比如 `|` 用于合并两个选择器的结果,`*` 代表零个或多个子节点,`[条件]` 用于过滤结果。 在实际应用中,XPath Helper这样的工具能帮助开发者验证XPath表达式的正确性,实时查看选取的节点,从而快速...
- **多线程安全**:ORMLite库是线程安全的,可以在多线程环境中放心使用。 4. **ORMLite的局限与优化**: - **性能**:相比于原生的SQL语句,ORM的性能可能略低,尤其是在大量数据操作时。可以通过合理设计数据库...
vmware-tray-helper.dll
Video DownloadHelper 需要安装后使用的插件Video DownloadHelper Companion App 1.4.0.dmg(花了一星期才下载下来,亲测可用)
2.再下载这个coapp,就可以直接下载使用。 安装成功后显示: Found companion app: VdhCoApp 1.3.0 Companion app binary: /Applications/...
maven-helper-plugin-1.0.3.jar
Video DownloadHelper 需要安装后使用的插件Video DownloadHelper Companion App 1.4.0.pkg(花了一星期才下载下来,亲测可用)
Mac系统下火狐,chrome浏览器downloadhelper 的指定配合下载APP。支持各大主流,非主流在线平台的视频下载。
5. **juh-5.4.2.jar**:Java UNO Helper库,包含了处理UNO接口和异常的辅助类,是使用OpenOffice API的基础。 6. **jodconverter-core-3.0-beta-4.jar** 和 **jodconverter-core-4.1.0.jar**:JODConverter是一个...
java运行依赖jar包
2.再下载这个coapp,就可以直接下载使用。 安装成功后显示: Found companion app: VdhCoApp 1.3.0 Companion app binary: /Applications/...
"Helper-master.zip" 是一个包含了各种常用帮助类和公共类的集合,旨在为开发者提供一个统一的、方便的资源库,以简化他们在编程过程中的工作。这个压缩包的出现是为了应对网上散乱的帮助类,它将这些实用工具类整理...
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装