效果图
下载文件:
讲解: 多线程下载多个文件:
下载首先就必须明白http协议,不会的读者:请点击这里 首先我们知道关于操作网络我们是不能直接在UI(主)线程中直接操作的,所以这里我们就得开启线程
在主线程调用的时候new多次开启的线程,就实现了线程,然后在每个线程中进行网络操作,这就是所谓的多线程下载多个文件。
1.首先在manifest中加入网络,允许读取文件等权限:
<!-- 访问 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"/>
2.activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ProgressBar style="?android:attr/progressBarStyleHorizontal"
android:layout_height="wrap_content" android:id="@+id/progressBar1"
android:layout_width="match_parent"></ProgressBar>
<TextView android:layout_width="match_parent" android:id="@+id/textView1"
android:layout_height="wrap_content" />
<ProgressBar style="?android:attr/progressBarStyleHorizontal"
android:layout_height="wrap_content" android:id="@+id/progressBar2"
android:layout_width="match_parent"></ProgressBar>
<TextView android:layout_width="match_parent" android:id="@+id/textView2"
android:layout_height="wrap_content" />
</LinearLayout>
3.MainActivity:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private ProgressBar pb1 = null;
private TextView tv1 = null;
private ProgressBar pb2 = null;
private TextView tv2 = null;
//得到SD的路径绝对路径
private String root = Environment.getExternalStorageDirectory();
//下载地址1
private String downloadFile = "http://gongxue.cn/yingyinkuaiche/UploadFiles_9323/201008/2010082909434077.mp3";
//下载地址2
private String downloadFile1 = "http://gongxue.cn/yingyinkuaiche/UploadFiles_9323/201008/2010082909434077.mp3";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb1 = (ProgressBar) findViewById(R.id.progressBar1);
tv1 = (TextView) findViewById(R.id.textView1);
pb2 = (ProgressBar) findViewById(R.id.progressBar2);
tv2 = (TextView) findViewById(R.id.textView2);
download(downloadFile, root, pb1, tv1);
download(downloadFile1, root, pb2, tv2);
}
private void download(String url, String targetPath, ProgressBar pb,
TextView tv) {
DownloadThread dt = new DownloadThread(url, targetPath, pb, tv);
//线程开始,下载开始
dt.start();
}
/**
* 自定义一个Handler类,处理线程消息
*/
public class MyHandler extends Handler {
private ProgressBar progressBar;
private TextView textView;
// 通过构造函数来确定给哪个ProgressBar刷新
public MyHandler(ProgressBar progressBar, TextView textView) {
this.progressBar = progressBar;
this.textView = textView;
}
public void handleMessage(Message msg) {
this.progressBar.setProgress(msg.arg1);
this.textView.setText(msg.arg1 + "%");
super.handleMessage(msg);
}
}
// 下载线程
public class DownloadThread extends Thread {
private String url = "";//下载地址
private String targetPath = "";//目标路径
private int hasDownload = 0; //已经下载的长度
private int len = -1;
private byte buffer[] = new byte[4 * 1024];
private int size = 0; //文件的长度
private int rate = 0;//所占的百分比
private MyHandler myHandler = null;
private Message msg = null;
private ProgressBar pb = null;
private TextView tv = null;
public DownloadThread(String url, String targetPath, ProgressBar pb,
TextView tv) {
this.url = url;
this.targetPath = targetPath;
this.pb = pb;
this.tv = tv;
myHandler = new MyHandler(this.pb, this.tv);
}
public void run() {
// 目标路径/文件名
String targetFileName = this.targetPath
+ this.url.substring(this.url.lastIndexOf("/") + 1,
this.url.length());
//文件对象
File downloadFile = new File(targetFileName);
if (!downloadFile.exists()) {
try {
downloadFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
URL fileUrl = new URL(this.url);
HttpURLConnection conn = (HttpURLConnection) fileUrl
.openConnection();
// 获取文件大小
size = conn.getContentLength();
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(targetFileName);
while ((len = is.read(buffer)) != -1) {
os.write(buffer);
hasDownload += len;
rate = (hasDownload * 100 / size);
msg = new Message();
msg.arg1 = rate;
myHandler.sendMessage(msg);
System.out.println(rate + "%");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}