您当前的位置: 首页 >  android

xiangzhihong8

暂无认证

  • 5浏览

    0关注

    1324博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

android多线程下载1

xiangzhihong8 发布时间:2013-12-03 09:08:29 ,浏览量:5

想做一个下载功能,当然理想的功能要支持多任务同时下载,断点续传的功能,我想一步一步来,首先困难摆在了多任务这里

开始我的思路是在一个Service中启动下载的流操作,然后通过Service中声明一个Activity中的Handler更新UI(比如进度条。。。)

可是我发现在Service中声明一个Activity中的Handler是做不到的(可能只是我做不到吧,无法申请内存)

于是,我决定在Activity中直接启动线程,让其运行,调用自身的Handler来更新UI,没想到在这个下载Activity onPause()的时候,线程还是活的,也就是说可以继续下载,下例是我做的一个两个任务同时下载的小例子,后面会把理想中的功能都陆续添加上的...

main.xml配置:

[html] view plain copy
  1.   
  2.   
  3.       
  4.   
  5.       
  6.       
  7.   
  8.       
  9.   
AndroidManifest.xml配置:

[html] view plain copy
  1.   
  2.   
  3.       
  4.   
  5.       
  6.           
  7.               
  8.                   
  9.                   
  10.               
  11.           
  12.   
  13.       
  14.       
  15.       
  16.       
  17.       
  18.       
  19.       
  20.       
  21.       
  22.       
  23.   
activity程序:

[java] view plain copy
  1. package sms.down;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.io.OutputStream;  
  8. import java.net.HttpURLConnection;  
  9. import java.net.MalformedURLException;  
  10. import java.net.URL;  
  11. import android.app.Activity;  
  12. import android.os.Bundle;  
  13. import android.os.Environment;  
  14. import android.os.Handler;  
  15. import android.os.Message;  
  16. import android.widget.ProgressBar;  
  17. import android.widget.TextView;  
  18.   
  19. public class MulThreadDownload extends Activity {  
  20.     /** Called when the activity is first created. */  
  21.     private ProgressBar pb1 = null;  
  22.     private TextView tv1 = null;  
  23.     private ProgressBar pb2 = null;  
  24.     private TextView tv2 = null;  
  25.   
  26.     private String root = Environment.getExternalStorageDirectory()  
  27.             .getAbsolutePath() + File.separator;  
  28.     private String downloadFile = "http://gongxue.cn/yingyinkuaiche/UploadFiles_9323/201008/2010082909434077.mp3";  
  29.     private String downloadFile1 = "http://gongxue.cn/yingyinkuaiche/UploadFiles_9323/201008/2010082909434077.mp3";  
  30.   
  31.     // 声明已经读过的长度变量  
  32.     private int hasRead = 0;  
  33.   
  34.     public void onCreate(Bundle savedInstanceState) {  
  35.         super.onCreate(savedInstanceState);  
  36.         setContentView(R.layout.main);  
  37.   
  38.         pb1 = (ProgressBar) findViewById(R.id.progressBar1);  
  39.         tv1 = (TextView) findViewById(R.id.textView1);  
  40.   
  41.         pb2 = (ProgressBar) findViewById(R.id.progressBar2);  
  42.         tv2 = (TextView) findViewById(R.id.textView2);  
  43.   
  44.         download(downloadFile, root, pb1, tv1);  
  45.   
  46.         download(downloadFile1, root, pb2, tv2);  
  47.     }  
  48.   
  49.     private void download(String url, String targetPath, ProgressBar pb,  
  50.             TextView tv) {  
  51.         DownloadThread dt = new DownloadThread(url, targetPath, pb, tv);  
  52.   
  53.         dt.start();  
  54.     }  
  55.   
  56.     // 自定义一个Handler类,处理线程消息  
  57.     public class MyHandler extends Handler {  
  58.         private ProgressBar progressBar;  
  59.         private TextView textView;  
  60.   
  61.         // 通过构造函数来确定给哪个ProgressBar刷新  
  62.         public MyHandler(ProgressBar progressBar, TextView textView) {  
  63.             this.progressBar = progressBar;  
  64.             this.textView = textView;  
  65.         }  
  66.   
  67.         public void handleMessage(Message msg) {  
  68.             this.progressBar.setProgress(msg.arg1);  
  69.             this.textView.setText(msg.arg1 + "%");  
  70.   
  71.             super.handleMessage(msg);  
  72.         }  
  73.     }  
  74.   
  75.     // 下载线程  
  76.     public class DownloadThread extends Thread {  
  77.         private String url = "";  
  78.         private String targetPath = "";  
  79.   
  80.         private int hasDownload = 0;  
  81.   
  82.         private int len = -1;  
  83.         private byte buffer[] = new byte[4 * 1024];  
  84.         private int size = 0;  
  85.         private int rate = 0;  
  86.   
  87.         private MyHandler myHandler = null;  
  88.         private Message msg = null;  
  89.   
  90.         private ProgressBar pb = null;  
  91.         private TextView tv = null;  
  92.   
  93.         public DownloadThread(String url, String targetPath, ProgressBar pb,  
  94.                 TextView tv) {  
  95.             this.url = url;  
  96.             this.targetPath = targetPath;  
  97.   
  98.             this.pb = pb;  
  99.             this.tv = tv;  
  100.   
  101.             myHandler = new MyHandler(this.pb, this.tv);  
  102.         }  
  103.   
  104.         public void run() {  
  105.             String targetFileName = this.targetPath  
  106.                     + this.url.substring(this.url.lastIndexOf("/") + 1,  
  107.                             this.url.length());  
  108.             File downloadFile = new File(targetFileName);  
  109.   
  110.             if (!downloadFile.exists()) {  
  111.                 try {  
  112.                     downloadFile.createNewFile();  
  113.                 } catch (IOException e) {  
  114.                     // TODO Auto-generated catch block  
  115.                     e.printStackTrace();  
  116.                 }  
  117.             }  
  118.   
  119.             try {  
  120.                 URL fileUrl = new URL(this.url);  
  121.                 HttpURLConnection conn = (HttpURLConnection) fileUrl  
  122.                         .openConnection();  
  123.   
  124.                 // 获取文件大小  
  125.                 size = conn.getContentLength();  
  126.   
  127.                 InputStream is = conn.getInputStream();  
  128.   
  129.                 OutputStream os = new FileOutputStream(targetFileName);  
  130.   
  131.                 while ((len = is.read(buffer)) != -1) {  
  132.                     os.write(buffer);  
  133.   
  134.                     hasDownload += len;  
  135.   
  136.                     rate = (hasDownload * 100 / size);  
  137.   
  138.                     msg = new Message();  
  139.   
  140.                     msg.arg1 = rate;  
  141.   
  142.                     myHandler.sendMessage(msg);  
  143.   
  144.                     System.out.println(rate + "%");  
  145.                 }  
  146.             } catch (MalformedURLException e) {  
  147.                 // TODO Auto-generated catch block  
  148.                 e.printStackTrace();  
  149.             } catch (IOException e) {  
  150.                 e.printStackTrace();  
  151.             }  
  152.   
  153.         }  
  154.     }  
  155.   
  156. }  
程序运行效果:

源码下载

下一步将实现断点续传和暂停删除开始事件,更多更新敬请关注....
关注
打赏
1482932726
查看更多评论
立即登录/注册

微信扫码登录

0.0499s