Android 之多线程断点续传之(一)

来源:互联网 发布:网易课堂mac版 编辑:程序博客网 时间:2024/04/28 07:05
  1. 第一步:从布局文件下手.main.xml:
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:tools="http://schemas.android.com/tools"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     android:orientation="vertical" >


  7.     <ProgressBar
  8.         android:id="@+id/progressBar1"
  9.         style="?android:attr/progressBarStyleHorizontal"
  10.         android:layout_width="fill_parent"
  11.         android:layout_height="wrap_content" >
  12.     </ProgressBar>


  13.     <TextView
  14.         android:id="@+id/textView1"
  15.         android:layout_width="fill_parent"
  16.         android:layout_height="wrap_content" />


  17.     <ProgressBar
  18.         android:id="@+id/progressBar2"
  19.         style="?android:attr/progressBarStyleHorizontal"
  20.         android:layout_width="fill_parent"
  21.         android:layout_height="wrap_content" >
  22.     </ProgressBar>


  23.     <TextView
  24.         android:id="@+id/textView2"
  25.         android:layout_width="fill_parent"
  26.         android:layout_height="wrap_content" />

  27. </LinearLayout>
  28.  <!-- 访问 internet 权限 -->  
        <uses-permission android:name="android.permission.INTERNET" />  
        <uses-permission android:name="android.permission.CALL_PHONE" />  
        <uses-permission android:name="android.permission.SEND_SMS" />  
        <uses-permission android:name="android.permission.READ_CONTACTS"/>  
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
        <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />  
        <uses-permission android:name="android.permission.READ_PHONE_STATE"/> 

  1. 第二步:看看Activity
  2. public class MainActivity extends Activity {
  3. private ProgressBar pb1 = null;
  4. private TextView tv1 = null; 
  5. private ProgressBar pb2 = null;
  6. private TextView tv2 = null;
  7. private String root = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
  8. private String downloadFile = "http://gongxue.cn/yingyinkuaiche/UploadFiles_9323/201008/2010082909434077.mp3"; 
  9. private String downloadFile1 = "http://gongxue.cn/yingyinkuaiche/UploadFiles_9323/201008/2010082909434077.mp3";
  10. // 声明已经读过的长度变量  
  11. private int hasRead = 0;  
  12.  
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. pb1 = (ProgressBar) findViewById(R.id.progressBar1);  
  18. tv1 = (TextView) findViewById(R.id.textView1);  
  19. //
  20.  pb2 = (ProgressBar) findViewById(R.id.progressBar2);  
  21.  tv2 = (TextView) findViewById(R.id.textView2);
  22.  //
  23.  download(downloadFile, root, pb1, tv1); 
  24.  download(downloadFile1, root, pb2, tv2);  
  25. }


  26. private void download(String url, String targetPath, ProgressBar pb,
  27. TextView tv) {
  28. //构造方法
  29. DownloadThread dt = new DownloadThread(url, targetPath, pb, tv);  
  30.   
  31.        dt.start();  
  32. }
  33. // 自定义一个Handler类,处理线程消息  
  34.     public class MyHandler extends Handler {  
  35.         private ProgressBar progressBar;  
  36.         private TextView textView;  
  37.   
  38.         // 通过构造函数来确定给哪个ProgressBar刷新  
  39.         public MyHandler(ProgressBar progressBar, TextView textView) {  
  40.             this.progressBar = progressBar;  
  41.             this.textView = textView;  
  42.         }  
  43.   
  44.         public void handleMessage(Message msg) {  
  45.             this.progressBar.setProgress(msg.arg1);  
  46.             this.textView.setText(msg.arg1 + "%");  
  47.   
  48.             super.handleMessage(msg);  
  49.         }  
  50.     }  
  51. // 下载线程  
  52.     public class DownloadThread extends Thread {  
  53.         private String url = "";  
  54.         private String targetPath = "";  
  55.   
  56.         private int hasDownload = 0;  
  57.   
  58.         private int len = -1;  
  59.         private byte buffer[] = new byte[4 * 1024];  
  60.         private int size = 0;  
  61.         private int rate = 0;  
  62.   
  63.         private MyHandler myHandler = null;  
  64.         private Message msg = null;  
  65.   
  66.         private ProgressBar pb = null;  
  67.         private TextView tv = null;  
  68.   
  69.         public DownloadThread(String url, String targetPath, ProgressBar pb,  
  70.                 TextView tv) {  
  71.             this.url = url;  
  72.             this.targetPath = targetPath;  
  73.   
  74.             this.pb = pb;  
  75.             this.tv = tv;  
  76.   
  77.             myHandler = new MyHandler(this.pb, this.tv);  
  78.         }  
  79.   
  80.         public void run() {  
  81.             String targetFileName = this.targetPath  
  82.                     + this.url.substring(this.url.lastIndexOf("/") + 1,  
  83.                             this.url.length());  
  84.             File downloadFile = new File(targetFileName);  
  85.   
  86.             if (!downloadFile.exists()) {  
  87.                 try {  
  88.                     downloadFile.createNewFile();  
  89.                 } catch (IOException e) {  
  90.                     // TODO Auto-generated catch block  
  91.                     e.printStackTrace();  
  92.                 }  
  93.             }  
  94.   
  95.             try {  
  96.                 URL fileUrl = new URL(this.url);  
  97.                 HttpURLConnection conn = (HttpURLConnection) fileUrl  
  98.                         .openConnection();  
  99.   
  100.                 // 获取文件大小  
  101.                 size = conn.getContentLength();  
  102.   
  103.                 InputStream is = conn.getInputStream();  
  104.   
  105.                 OutputStream os = new FileOutputStream(targetFileName);  
  106.   
  107.                 while ((len = is.read(buffer)) != -1) {  
  108.                     os.write(buffer);  
  109.   
  110.                     hasDownload += len;  
  111.   
  112.                     rate = (hasDownload * 100 / size);  
  113.   
  114.                     msg = new Message();  
  115.   
  116.                     msg.arg1 = rate;  
  117.   
  118.                     myHandler.sendMessage(msg);  
  119.   
  120.                     System.out.println(rate + "%");  
  121.                 }  
  122.             } catch (MalformedURLException e) {  
  123.                 // TODO Auto-generated catch block  
  124.                 e.printStackTrace();  
  125.             } catch (IOException e) {  
  126.                 e.printStackTrace();  
  127.             }  
  128.   
  129.         }  
  130.     }  


  131. }

  132. 最后看看效果图吧:


0 0