现在的位置: 首页 > 综合 > 正文

实现多线程下载文件

2013年08月12日 ⁄ 综合 ⁄ 共 1370字 ⁄ 字号 评论关闭

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

 

首先看下效果图

 

 

创建工程 ThreadDemo

 

首先布局文件 threaddemo.xml

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="下载地址"  
  11.     />  
  12. <TextView  
  13.     android:id="@+id/downloadurl"  
  14.     android:layout_width="fill_parent"   
  15.     android:layout_height="wrap_content"   
  16.     android:lines="5"  
  17.     />  
  18. <TextView    
  19.     android:layout_width="fill_parent"   
  20.     android:layout_height="wrap_content"   
  21.     android:text="线程数"  
  22.     />  
  23. <EditText  
  24.     android:id="@+id/downloadnum"  
  25.     android:layout_width="fill_parent"   
  26.     android:layout_height="wrap_content"   
  27.     />  
  28. <ProgressBar  
  29.     android:id="@+id/downloadProgressBar"  
  30.     android:layout_width="fill_parent"   
  31.     style="?android:attr/progressBarStyleHorizontal"  
  32.     android:layout_height="wrap_content"   
  33.     />  
  34. <TextView  
  35.     android:id="@+id/downloadinfo"  
  36.     android:layout_width="fill_parent"   
  37.     android:layout_height="wrap_content"   
  38.     android:text="下载进度 0"  
  39.     />  
  40. <Button  
  41.     android:id="@+id/downloadbutton"  
  42.     android:layout_width="wrap_content"   

抱歉!评论已关闭.