Android版多线程下载器核心代码分享

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android版多线程下载器核心代码分享相关的知识,希望对你有一定的参考价值。

首先给大家分享多线程下载核心类:

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

下面是界面的逻辑代码:

技术分享
  1 package com.example.urltest;
  2 
  3 import android.app.Activity;
  4 import android.app.AlertDialog;
  5 import android.app.ProgressDialog;
  6 import android.content.DialogInterface;
  7 import android.os.Bundle;
  8 import android.os.Message;
  9 import android.view.View;
 10 import android.view.View.OnClickListener;
 11 import android.widget.Button;
 12 import android.widget.EditText;
 13 import android.widget.ProgressBar;
 14 import android.widget.TextView;
 15 import android.widget.Toast;
 16 
 17 import java.io.IOException;
 18 import java.util.Timer;
 19 import java.util.TimerTask;
 20 
 21 public class MultiThreadDown extends Activity {
 22     EditText url, target;
 23     Button downButton;
 24     ProgressBar bar;
 25     ProgressDialog progressDialog;
 26     View downView;
 27     DownUtil downUtil;
 28     private int mDownStatus;
 29     private int threadNum = 6; // 默认的线程数
 30     android.os.Handler handler = new android.os.Handler() {
 31 
 32         @Override
 33         public void handleMessage(Message msg) {
 34             if (msg.what == 0x123) {
 35                 bar.setProgress(mDownStatus);
 36                 if (mDownStatus >= 100) {
 37                     Toast.makeText(MultiThreadDown.this, "下载完成", Toast.LENGTH_SHORT).show();
 38                 }
 39                 // Log.i("csx", "" + mDownStatus);
 40 
 41             }
 42         }
 43 
 44     };
 45 
 46     @Override
 47     protected void onCreate(Bundle savedInstanceState) {
 48 
 49         super.onCreate(savedInstanceState);
 50         setContentView(R.layout.layout_down);
 51         url = (EditText) findViewById(R.id.url);
 52 
 53         downButton = (Button) findViewById(R.id.down);
 54         bar = (ProgressBar) findViewById(R.id.bar);
 55         progressDialog = new ProgressDialog(this);
 56         progressDialog.setTitle("尝试连接");
 57         progressDialog.setMessage("正在连接...");
 58         downButton.setOnClickListener(new DownButtonOnClickListener());
 59 
 60     }
 61 
 62     private class DownButtonOnClickListener implements OnClickListener {
 63 
 64         EditText targetFilePath, fileName;
 65         TextView fileSize;
 66         Thread connectionThread;
 67 
 68         public Thread instanceOfConnectionThread() {
 69             return new Thread() {
 70 
 71                 @Override
 72                 public void run() {
 73                     try {
 74                         downUtil.getFileInformation();
 75 
 76                     } catch (IOException e1) {
 77                         e1.printStackTrace();
 78                     }
 79 
 80                 }
 81 
 82             };
 83         }
 84 
 85         @Override
 86         public void onClick(View v) {
 87 
 88             String urlPath = url.getText().toString();
 89             if (urlPath == null || urlPath.equals("")) {
 90                 return;
 91             }
 92             progressDialog.show();
 93             downUtil = new DownUtil(urlPath, threadNum);
 94             connectionThread = instanceOfConnectionThread();
 95             connectionThread.start();
 96 
 97             int connectionNum = 3;
 98             while (!downUtil.isGetFileInformation() && connectionNum > 0) {// 循环请求连接,如果3次之后还没有连接成功,就退出
 99                 if (!connectionThread.isAlive()) {
100                     connectionThread = null;
101                     connectionThread = instanceOfConnectionThread();
102                     connectionThread.start();
103                     connectionNum--;
104                 }
105 
106             }
107 
108             progressDialog.cancel();
109             if (!downUtil.isGetFileInformation()) {
110                 Toast.makeText(MultiThreadDown.this, "请求失败!", Toast.LENGTH_SHORT).show();
111                 return;
112             }
113             downView = getLayoutInflater().inflate(R.layout.layout_download_view, null);
114             targetFilePath = (EditText) downView.findViewById(R.id.editText_target_path);
115             fileName = (EditText) downView.findViewById(R.id.editText_file_name);
116             fileSize = (TextView) downView.findViewById(R.id.textView_file_size);
117             targetFilePath.setText(downUtil.getDefaultTargetPath());
118             fileName.setText(downUtil.getFileName());
119             fileSize.append("" + ((double) downUtil.getFileSize()) / 1024 + "k");
120 
121             new AlertDialog.Builder(MultiThreadDown.this).setView(downView)
122                     .setPositiveButton("确定", new DialogInterface.OnClickListener() {
123 
124                         @Override
125                         public void onClick(DialogInterface dialog, int which) {
126                             if (!downUtil.isGetFileInformation()) {
127                                 dialog.dismiss();
128                                 return;
129                             }
130                             final String path = targetFilePath.getText().toString();
131                             final String name = fileName.getText().toString();
132 
133                             new Thread() {
134 
135                                 @Override
136                                 public void run() {
137                                     try {
138                                         downUtil.download(path, name);
139                                     } catch (IOException e) {
140                                         // TODO Auto-generated catch block
141                                         e.printStackTrace();
142                                     }
143 
144                                     final Timer timer = new Timer();
145                                     TimerTask task = new TimerTask() {
146 
147                                         @Override
148                                         public void run() {
149 
150                                             mDownStatus = (int) (downUtil.getCompleteRate() * 100);
151                                             handler.sendEmptyMessage(0x123);
152                                             if (mDownStatus >= 100) {
153                                                 timer.cancel();
154                                             }
155 
156                                         }
157                                     };
158                                     timer.schedule(task, 0, 100);
159 
160                                 }
161 
162                             }.start();
163 
164                         }
165                     }).setNegativeButton("取消", null)
166                     .setTitle(downUtil.isGetFileInformation() ? "链接可用" : "链接不可用").show();
167 
168         }
169     }
170 
171 }
技术分享

下面是主页面布局:layout_down.xml

技术分享
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6 
 7     <ScrollView
 8         android:id="@+id/scrollView1"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content" >
11 
12         <LinearLayout
13             android:layout_width="match_parent"
14             android:layout_height="match_parent"
15             android:orientation="vertical" >
16 
17             <TextView
18                 android:layout_width="match_parent"
19                 android:layout_height="wrap_content"
20                 android:text="要下载的资源的URL:" />
21 
22             <EditText
23                 android:id="@+id/url"
24                 android:layout_width="match_parent"
25                 android:layout_height="wrap_content"
26                 android:text="在在这里输入URL" />
27 
28             <Button
29                 android:id="@+id/down"
30                 android:layout_width="match_parent"
31                 android:layout_height="wrap_content"
32                 android:text="下载" />
33             <!-- 定义一个水平进度条,用于显示下载进度 -->
34 
35             <ProgressBar
36                 android:id="@+id/bar"
37                 style="?android:attr/progressBarStyleHorizontal"
38                 android:layout_width="match_parent"
39                 android:layout_height="wrap_content"
40                 android:max="100" />
41         </LinearLayout>
42     </ScrollView>
43 
44 </LinearLayout>
技术分享

下面是下载选项dialog布局:layout_download_view.xml

技术分享
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6 
 7     <TextView
 8         android:id="@+id/textView1"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:text="下载路径:" />
12 
13     <EditText
14         android:id="@+id/editText_target_path"
15         android:layout_width="match_parent"
16         android:layout_height="wrap_content"
17         android:ems="10" >
18 
19         <requestFocus />
20     </EditText>
21 
22     <TextView
23         android:id="@+id/textView2"
24         android:layout_width="wrap_content"
25         android:layout_height="wrap_content"
26         android:text="文件名:" />
27 
28     <EditText
29         android:id="@+id/editText_file_name"
30         android:layout_width="match_parent"
31         android:layout_height="wrap_content"
32         android:maxLines="1"
33         android:ems="10" />
34 
35     <TextView
36         android:id="@+id/textView_file_size"
37         android:layout_width="wrap_content"
38         android:layout_height="wrap_content"
39         android:text="文件大小:" />
40 
41 </LinearLayout>
技术分享

效果图如下:输入URL,点击下载弹出对话框,输入路径和文件名 点击确定开始下载

技术分享

技术分享

以上是关于Android版多线程下载器核心代码分享的主要内容,如果未能解决你的问题,请参考以下文章

#私藏项目实操分享#Java多线程下载器项目实例之线程池

Android多线程断点下载的代码流程解析

多线程无损音乐下载器了解一下?

如何使用Android片段管理器传递变量[重复]

Android 逆向类加载器 ClassLoader ( 类加载器源码简介 | BaseDexClassLoader | DexClassLoader | PathClassLoader )(代码片段

Android - 片段中的联系人选择器