Android开发--多线程下载加断点续传

上传者: qq396371485 | 上传时间: 2022-05-17 14:53:21 | 文件大小: 1.94MB | 文件类型: RAR
1.多线程下载: 首先通过下载总线程数来划分文件的下载区域:利用int range = fileSize / threadCount;得到每一段下载量;每一段的位置是i * range到(i + 1) * rang - 1,注意最后一段的位置是到filesize - 1; 通过Http协议的Range字段实现下载文件的分段; 通过Java类RandomAccessFile可以实现文件的随机访问,利用seek方法定位的文件的指定位置; 由HttpUrlConnection获取流来进行流的读写,实现文件的存储; 在下载过程中利用Handler来向外传递下载的信息。 2.断点续传: 对于每一个线程利用一个DownloadInfo类来保存下载的信息,每次在下载过程中向数据库更新信息(我也有想过只在下载暂停时进行更新,但那样的话我们的进程被杀掉时信息就无法保存下来)。在进行下载之前去访问数据库是否有记录存在,如果没有执行第一次下载的初始化,如果存在记录但下载文件不存在时,删掉数据库中的记录之后进行第一次下载的初始化,如果有记录且文件存在,则从数据库中取出信息。 实现的效果如图,自己封装的类提供了开始,暂停,删除,以及重新下载的方法。

文件下载

资源详情

[{"title":"( 72 个子文件 1.94MB ) Android开发--多线程下载加断点续传","children":[{"title":"Breakpoint_Download_File","children":[{"title":".project <span style='color:#111;'> 860B </span>","children":null,"spread":false},{"title":"project.properties <span style='color:#111;'> 609B </span>","children":null,"spread":false},{"title":"src","children":[{"title":"com","children":[{"title":"example","children":[{"title":"test","children":[{"title":"DownloadUtil.java <span style='color:#111;'> 3.17KB </span>","children":null,"spread":false},{"title":"MainActivity.java <span style='color:#111;'> 4.53KB </span>","children":null,"spread":false},{"title":"DownloadInfo.java <span style='color:#111;'> 1.42KB </span>","children":null,"spread":false},{"title":"DownLoadHelper.java <span style='color:#111;'> 1014B </span>","children":null,"spread":false},{"title":"DownloadHttpTool.java <span style='color:#111;'> 6.48KB </span>","children":null,"spread":false},{"title":"DownlaodSqlTool.java <span style='color:#111;'> 2.16KB </span>","children":null,"spread":false}],"spread":true}],"spread":true}],"spread":true}],"spread":true},{"title":"AndroidManifest.xml <span style='color:#111;'> 1.13KB </span>","children":null,"spread":false},{"title":"res","children":[{"title":"drawable-ldpi","children":null,"spread":false},{"title":"drawable-hdpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 7.48KB </span>","children":null,"spread":false}],"spread":true},{"title":"values-v11","children":[{"title":"styles.xml <span style='color:#111;'> 334B </span>","children":null,"spread":false}],"spread":true},{"title":"menu","children":[{"title":"main.xml <span style='color:#111;'> 263B </span>","children":null,"spread":false}],"spread":true},{"title":"drawable-mdpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 3.69KB </span>","children":null,"spread":false}],"spread":true},{"title":"layout","children":[{"title":"activity_main.xml <span style='color:#111;'> 2.06KB </span>","children":null,"spread":false}],"spread":true},{"title":"values-sw600dp","children":[{"title":"dimens.xml <span style='color:#111;'> 203B </span>","children":null,"spread":false}],"spread":true},{"title":"drawable-xxhdpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 24.20KB </span>","children":null,"spread":false}],"spread":true},{"title":"drawable-xhdpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 12.22KB </span>","children":null,"spread":false}],"spread":false},{"title":"values","children":[{"title":"strings.xml <span style='color:#111;'> 221B </span>","children":null,"spread":false},{"title":"styles.xml <span style='color:#111;'> 697B </span>","children":null,"spread":false},{"title":"dimens.xml <span style='color:#111;'> 220B </span>","children":null,"spread":false}],"spread":false},{"title":"values-sw720dp-land","children":[{"title":"dimens.xml <span style='color:#111;'> 277B </span>","children":null,"spread":false}],"spread":false},{"title":"values-v14","children":[{"title":"styles.xml <span style='color:#111;'> 391B </span>","children":null,"spread":false}],"spread":false}],"spread":false},{"title":".settings","children":[{"title":"org.eclipse.core.resources.prefs <span style='color:#111;'> 113B </span>","children":null,"spread":false}],"spread":true},{"title":"assets","children":null,"spread":false},{"title":"gen","children":[{"title":"com","children":[{"title":"example","children":[{"title":"test","children":[{"title":"BuildConfig.java <span style='color:#111;'> 158B </span>","children":null,"spread":false},{"title":"R.java <span style='color:#111;'> 162.85KB </span>","children":null,"spread":false}],"spread":true}],"spread":true}],"spread":true}],"spread":true},{"title":"libs","children":[{"title":"android-support-v4.jar <span style='color:#111;'> 543.16KB </span>","children":null,"spread":false}],"spread":true},{"title":".classpath <span style='color:#111;'> 380B </span>","children":null,"spread":false},{"title":"proguard-project.txt <span style='color:#111;'> 781B </span>","children":null,"spread":false},{"title":"ic_launcher-web.png <span style='color:#111;'> 50.19KB </span>","children":null,"spread":false},{"title":"bin","children":[{"title":"resources.ap_ <span style='color:#111;'> 278.19KB </span>","children":null,"spread":false},{"title":"R.txt <span style='color:#111;'> 29.01KB </span>","children":null,"spread":false},{"title":"Breakpoint_Download_File.apk <span style='color:#111;'> 649.84KB </span>","children":null,"spread":false},{"title":"classes","children":[{"title":"com","children":[{"title":"example","children":[{"title":"test","children":[{"title":"R$layout.class <span style='color:#111;'> 1.59KB </span>","children":null,"spread":false},{"title":"R$style.class <span style='color:#111;'> 8.65KB </span>","children":null,"spread":false},{"title":"R$dimen.class <span style='color:#111;'> 1.31KB </span>","children":null,"spread":false},{"title":"MainActivity$4.class <span style='color:#111;'> 918B </span>","children":null,"spread":false},{"title":"BuildConfig.class <span style='color:#111;'> 341B </span>","children":null,"spread":false},{"title":"R$drawable.class <span style='color:#111;'> 5.07KB </span>","children":null,"spread":false},{"title":"R$attr.class <span style='color:#111;'> 4.80KB </span>","children":null,"spread":false},{"title":"DownloadUtil$1.class <span style='color:#111;'> 1.81KB </span>","children":null,"spread":false},{"title":"DownloadHttpTool$DownloadThread.class <span style='color:#111;'> 4.24KB </span>","children":null,"spread":false},{"title":"DownloadInfo.class <span style='color:#111;'> 1.88KB </span>","children":null,"spread":false},{"title":"R$bool.class <span style='color:#111;'> 727B </span>","children":null,"spread":false},{"title":"R$integer.class <span style='color:#111;'> 409B </span>","children":null,"spread":false},{"title":"MainActivity$5.class <span style='color:#111;'> 917B </span>","children":null,"spread":false},{"title":"DownloadHttpTool$Download_State.class <span style='color:#111;'> 1.25KB </span>","children":null,"spread":false},{"title":"R$id.class <span style='color:#111;'> 2.94KB </span>","children":null,"spread":false},{"title":"R$anim.class <span style='color:#111;'> 593B </span>","children":null,"spread":false},{"title":"DownloadUtil$2.class <span style='color:#111;'> 2.11KB </span>","children":null,"spread":false},{"title":"DownloadUtil$OnDownloadListener.class <span style='color:#111;'> 350B </span>","children":null,"spread":false},{"title":"MainActivity$2.class <span style='color:#111;'> 917B </span>","children":null,"spread":false},{"title":"R.class <span style='color:#111;'> 891B </span>","children":null,"spread":false},{"title":"R$string.class <span style='color:#111;'> 1.18KB </span>","children":null,"spread":false},{"title":"MainActivity$1.class <span style='color:#111;'> 3.22KB </span>","children":null,"spread":false},{"title":"MainActivity$AppReceiver.class <span style='color:#111;'> 1.45KB </span>","children":null,"spread":false},{"title":"MainActivity$3.class <span style='color:#111;'> 917B </span>","children":null,"spread":false},{"title":"R$menu.class <span style='color:#111;'> 382B </span>","children":null,"spread":false},{"title":"MainActivity.class <span style='color:#111;'> 3.98KB </span>","children":null,"spread":false},{"title":"DownLoadHelper.class <span style='color:#111;'> 1.31KB </span>","children":null,"spread":false},{"title":"DownlaodSqlTool.class <span style='color:#111;'> 3.41KB </span>","children":null,"spread":false},{"title":"R$color.class <span style='color:#111;'> 558B </span>","children":null,"spread":false},{"title":"DownloadHttpTool.class <span style='color:#111;'> 5.90KB </span>","children":null,"spread":false},{"title":"DownloadUtil.class <span style='color:#111;'> 2.54KB </span>","children":null,"spread":false},{"title":"R$styleable.class <span style='color:#111;'> 6.27KB </span>","children":null,"spread":false}],"spread":false}],"spread":false}],"spread":false}],"spread":false},{"title":"AndroidManifest.xml <span style='color:#111;'> 1.13KB </span>","children":null,"spread":false},{"title":"dexedLibs","children":[{"title":"android-support-v7-appcompat-f88b35db0d0ed9ed13ddf7525791bffa.jar <span style='color:#111;'> 142.94KB </span>","children":null,"spread":false},{"title":"android-support-v4-df5708a7e6c1b1e8c7f539cf4880135d.jar <span style='color:#111;'> 206.37KB </span>","children":null,"spread":false}],"spread":false},{"title":"res","children":[{"title":"crunch","children":[{"title":"drawable-hdpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 5.82KB </span>","children":null,"spread":false}],"spread":false},{"title":"drawable-mdpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 3.04KB </span>","children":null,"spread":false}],"spread":false},{"title":"drawable-xxhdpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 17.47KB </span>","children":null,"spread":false}],"spread":false},{"title":"drawable-xhdpi","children":[{"title":"ic_launcher.png <span style='color:#111;'> 9.14KB </span>","children":null,"spread":false}],"spread":false}],"spread":false}],"spread":false},{"title":"classes.dex <span style='color:#111;'> 1.21MB </span>","children":null,"spread":false}],"spread":true}],"spread":false}],"spread":true}]

评论信息

  • y1204234297 :
    不错,正好用到了
    2015-09-17

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明