Android实现多线程下载文件的方法

2019-12-10 19:58:34王冬梅

易采站长站为您分析Android实现多线程下载文件的方法,以实例形式较为详细的分析了Android多线程文件传输及合并等操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了Android实现多线程下载文件的方法。。具体如下:

多线程下载大概思路就是通过Range 属性实现文件分段,然后用RandomAccessFile 来读写文件,最终合并为一个文件

首先看下效果图:

Android实现多线程下载文件的方法

创建工程 ThreadDemo

首先布局文件 threaddemo.xml

 

 
  1. <?xml version="1.0" encoding="utf-8"?>   <LinearLayout xmlns:android="http://www.easck.com/apk/res/android" 
  2. android:orientation="vertical"  android:layout_width="fill_parent" 
  3. android:layout_height="fill_parent"  >  
  4. <TextView  android:layout_width="fill_parent" 
  5. android:layout_height="wrap_content"  android:text="下载地址" 
  6. />   <TextView 
  7. android:id="@+id/downloadurl"  android:layout_width="fill_parent" 
  8. android:layout_height="wrap_content"  android:lines="5" 
  9. />   <TextView 
  10. android:layout_width="fill_parent"  android:layout_height="wrap_content" 
  11. android:text="线程数"  />  
  12. <EditText  android:id="@+id/downloadnum" 
  13. android:layout_width="fill_parent"  android:layout_height="wrap_content" 
  14. />   <ProgressBar 
  15. android:id="@+id/downloadProgressBar"  android:layout_width="fill_parent" 
  16. style="?android:attr/progressBarStyleHorizontal"  android:layout_height="wrap_content" 
  17. />   <TextView 
  18. android:id="@+id/downloadinfo"  android:layout_width="fill_parent" 
  19. android:layout_height="wrap_content"  android:text="下载进度 0"