Android中多任务断点续传下载(一)

新年第一篇文章,先祝大家在新的一年里马到成功、马上有钱、马上有车、马上有房……

说下本篇文章的主要内容吧,去年项目中有个需求就是实现类似如App商城之类的功能,当然首先想到的就是多任务下载了,而且还要支持断点续传下载,其实类似的功能网上都能找到解决方法和相关的例子,但初看了些网上资料都不能很好的实现像安卓市场、GooglePlay下载部分的功能,其实很多广告SDK也提供了类似下载功能,但这终归是别人实现的,用着别人的东西终归不爽,感受自己劳动得来的成果是很happy的。后来在github上面搜到有位哥们写的很不错,这是是github上下载地址,我就是基于这个项目来实现的,在此感谢这位哥们的无私奉献的精神。

先看下我的实现效果,上图

    

先闲扯几句,不知道大家有没有跟我类似的感觉,我一般看别人的博客或者相关技术帖子的时候首先会找效果图,如果没效果图那么接下来看下去的欲望会大打折扣,不说别的,如果我看的就是这篇我写的文章,如果作者有图的话给我的第一赶脚会是“这就是我的功能”,一定要认真读下去,接着下源码,然后在手机上run看看……,然后给作者点个赞。

项目的大体思路是这样的,还是先看图吧

图简单的描述了大体思路,我简单描述下,下载开始后会将下载任务添加到Queue中,有个while循环隔1s会从Queue中取下载任务,取到了任务就开始下载并添加到List中来记录当前正在下载的任务,为了方便叙述这里用downloadingTask表示;暂停任务时首先会删除downloadingTask中相应的任务并将任务添加到暂停列表中,这里用pausedTask表示;继续下载任务时首先会删除pausedTask中相应的任务,然后将任务添加到downloadingTask中。

下一篇将针对代码来具体的叙述。

《Android中多任务断点续传下载(一)》上有3条评论

  1. 赞一个博主,表示非常感谢,我虽然还没下载源码看。这个思路一直是我想去实现却还没实现的,看到你这个后思路更开阔了一些。

  2. Hi Longdw, i try this project android-multiple-task-download: https://github.com/longdw/MultiTaskDownloaderi have problem:when i downloading (example download: 30% file apk) i do close app running (quit appplication) and show crash This log for crash:01-14 02:26:10.493: E/AndroidRuntime(1683): java.lang.RuntimeException: Unable to destroy activity {com.ldw.downloader/com.ldw.downloader.test.MainActivity}: java.lang.IllegalArgumentException: Service not registered: com.ldw.downloader.service.ServiceManager$1@40cfa8b801-14 02:26:10.493: E/AndroidRuntime(1683): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3451)01-14 02:26:10.493: E/AndroidRuntime(1683): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3469)01-14 02:26:10.493: E/AndroidRuntime(1683): at android.app.ActivityThread.access$1200(ActivityThread.java:141)01-14 02:26:10.493: E/AndroidRuntime(1683): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1287)01-14 02:26:10.493: E/AndroidRuntime(1683): at android.os.Handler.dispatchMessage(Handler.java:99)01-14 02:26:10.493: E/AndroidRuntime(1683): at android.os.Looper.loop(Looper.java:137)01-14 02:26:10.493: E/AndroidRuntime(1683): at android.app.ActivityThread.main(ActivityThread.java:5041)01-14 02:26:10.493: E/AndroidRuntime(1683): at java.lang.reflect.Method.invokeNative(Native Method)01-14 02:26:10.493: E/AndroidRuntime(1683): at java.lang.reflect.Method.invoke(Method.java:511)01-14 02:26:10.493: E/AndroidRuntime(1683): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)01-14 02:26:10.493: E/AndroidRuntime(1683): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)01-14 02:26:10.493: E/AndroidRuntime(1683): at dalvik.system.NativeStart.main(Native Method)01-14 02:26:10.493: E/AndroidRuntime(1683): Caused by: java.lang.IllegalArgumentException: Service not registered: com.ldw.downloader.service.ServiceManager$1@40cfa8b801-14 02:26:10.493: E/AndroidRuntime(1683): at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:921)01-14 02:26:10.493: E/AndroidRuntime(1683): at android.app.ContextImpl.unbindService(ContextImpl.java:1451)01-14 02:26:10.493: E/AndroidRuntime(1683): at android.content.ContextWrapper.unbindService(ContextWrapper.java:484)01-14 02:26:10.493: E/AndroidRuntime(1683): at com.ldw.downloader.service.ServiceManager.disConnectService(ServiceManager.java:59)01-14 02:26:10.493: E/AndroidRuntime(1683): at com.ldw.downloader.test.MainActivity.onDestroy(MainActivity.java:562)01-14 02:26:10.493: E/AndroidRuntime(1683): at android.app.Activity.performDestroy(Activity.java:5273)01-14 02:26:10.493: E/AndroidRuntime(1683): at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1110)01-14 02:26:10.493: E/AndroidRuntime(1683): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3438)01-14 02:26:10.493: E/AndroidRuntime(1683): … 11 morecould you hepl me how to fix this error?Thank you!

  3. Hi,From the last published articles has been a long period of time,you can check the problem with logs and debug the project.Sorry…

评论已关闭。