用 Handler 来实现异步消息处理,以一个可以实时汇报下载进度的异步下载类为例
开发一个 Android 类库,本例中此类库名为 webabcd_util
New -> Java Project
项目上点右键 -> Build Path -> Add Libraries -> User Library -> User Libraries -> New -> 为类库起个名字 -> 选中这个类库 -> Add JARs 导入 Android 的 jar 包
项目上点右键 -> Build Path -> Add Libraries -> User Library -> 选择 Android 库
DownloadManagerAsync.java
代码
package webabcd.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.protocol.HTTP;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
// 以一个实例,即异步下载,来演示 Android 的异步消息处理(用 Handler 的方式)
public class DownloadManagerAsync {
public DownloadManagerAsync() {
}
// 实例化自定义的 Handler
EventHandler mHandler = new EventHandler(this);
// 按指定 url 地址下载文件到指定路径
public void download(final String url, final String savePath) {
new Thread(new Runnable() {
public void run() {
try {
sendMessage(FILE_DOWNLOAD_CONNECT);
URL sourceUrl = new URL(url);
URLConnection conn = sourceUrl.openConnection();
InputStream inputStream = conn.getInputStream();
int fileSize = conn.getContentLength();
File savefile = new File(savePath);
if (savefile.exists()) {
savefile.delete();
}
savefile.createNewFile();
FileOutputStream outputStream = new FileOutputStream(
savePath, true);
byte[] buffer = new byte[1024];
int readCount = 0;
int readNum = 0;
int prevPercent = 0;
while (readCount < fileSize && readNum != -1) {
readNum = inputStream.read(buffer);
if (readNum > -1) {
outputStream.write(buffer);
readCount = readCount + readNum;
int percent = (int) (readCount * 100 / fileSize);
if (percent > prevPercent) {
// 发送下载进度信息
sendMessage(FILE_DOWNLOAD_UPDATE, percent,
readCount);
prevPercent = percent;
}
}
}
outputStream.close();
sendMessage(FILE_DOWNLOAD_COMPLETE, savePath);
} catch (Exception e) {
sendMessage(FILE_DOWNLOAD_ERROR, e);
Log.e("MyError", e.toString());
}
}
}).start();
}
// 读取指定 url 地址的响应内容
public void download(final String url) {
new Thread(new Runnable() {
public void run() {
try {
sendMessage(FILE_DOWNLOAD_CONNECT);
URL sourceUrl = new URL(url);
URLConnection conn = sourceUrl.openConnection();
conn.setConnectTimeout(3000);
BufferedReader reader = new BufferedReader(
new InputStreamReader(conn.getInputStream(),
HTTP.UTF_8));
String line = null;
StringBuffer content = new StringBuffer();
while ((line = reader.readLine()) != null) {
content.append(line);
}
reader.close();
sendMessage(FILE_DOWNLOAD_COMPLETE, content.toString());
} catch (Exception e) {
sendMessage(FILE_DOWNLOAD_ERROR, e);
Log.e("MyError", e.toString());
}
}
}).start();
}
// 向 Handler 发送消息
private void sendMessage(int what, Object obj) {
// 构造需要向 Handler 发送的消息
Message msg = mHandler.obtainMessage(what, obj);
// 发送消息
mHandler.sendMessage(msg);
}
private void sendMessage(int what) {
Message msg = mHandler.obtainMessage(what);
mHandler.sendMessage(msg);
}
private void sendMessage(int what, int arg1, int arg2) {
Message msg = mHandler.obtainMessage(what, arg1, arg2);
mHandler.sendMessage(msg);
}
private static final int FILE_DOWNLOAD_CONNECT = 0;
private static final int FILE_DOWNLOAD_UPDATE = 1;
private static final int FILE_DOWNLOAD_COMPLETE = 2;
private static final int FILE_DOWNLOAD_ERROR = -1;
// 自定义的 Handler
private class EventHandler extends Handler {
private DownloadManagerAsync mManager;
public EventHandler(DownloadManagerAsync manager) {
mManager = manager;
}
// 处理接收到的消息
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case FILE_DOWNLOAD_CONNECT:
if (mOnDownloadConnectListener != null)
mOnDownloadConnectListener.onDownloadConnect(mManager);
break;
case FILE_DOWNLOAD_UPDATE:
if (mOnDownloadUpdateListener != null)
mOnDownloadUpdateListener.onDownloadUpdate(mManager,
msg.arg1);
break;
case FILE_DOWNLOAD_COMPLETE:
if (mOnDownloadCompleteListener != null)
mOnDownloadCompleteListener.onDownloadComplete(mManager,
msg.obj);
break;
case FILE_DOWNLOAD_ERROR:
if (mOnDownloadErrorListener != null)
mOnDownloadErrorListener.onDownloadError(mManager,
(Exception) msg.obj);
break;
default:
break;
}
}
}
// 定义连接事件
private OnDownloadConnectListener mOnDownloadConnectListener;
public interface OnDownloadConnectListener {
void onDownloadConnect(DownloadManagerAsync manager);
}
public void setOnDownloadConnectListener(OnDownloadConnectListener listener) {
mOnDownloadConnectListener = listener;
}
// 定义下载进度更新事件
private OnDownloadUpdateListener mOnDownloadUpdateListener;
public interface OnDownloadUpdateListener {
void onDownloadUpdate(DownloadManagerAsync manager, int percent);
}
public void setOnDownloadUpdateListener(OnDownloadUpdateListener listener) {
mOnDownloadUpdateListener = listener;
}
// 定义下载完成事件
private OnDownloadCompleteListener mOnDownloadCompleteListener;
public interface OnDownloadCompleteListener {
void onDownloadComplete(DownloadManagerAsync manager, Object result);
}
public void setOnDownloadCompleteListener(
OnDownloadCompleteListener listener) {
mOnDownloadCompleteListener = listener;
}
// 定义下载异常事件
private OnDownloadErrorListener mOnDownloadErrorListener;
public interface OnDownloadErrorListener {
void onDownloadError(DownloadManagerAsync manager, Exception e);
}
public void setOnDownloadErrorListener(OnDownloadErrorListener listener) {
mOnDownloadErrorListener = listener;
}
}
调用上面的自定义的 Android 类库
项目上点右键 -> Properties -> Java Build Path -> Projects -> Add 引用上面的类库
Main.java
代码
package com.webabcd.handler;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import webabcd.util.DownloadManagerAsync;
public class Main extends Activity implements
DownloadManagerAsync.OnDownloadCompleteListener,
DownloadManagerAsync.OnDownloadUpdateListener,
DownloadManagerAsync.OnDownloadErrorListener {
TextView txt;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DownloadManagerAsync manager = new DownloadManagerAsync();
manager.setOnDownloadCompleteListener(this);
manager.setOnDownloadUpdateListener(this);
manager.download("http://files.cnblogs.com/webabcd/Android.rar", "/sdcard/Android.rar");
txt = (TextView) this.findViewById(R.id.txt);
txt.setText("开始下载");
}
public void onDownloadComplete(DownloadManagerAsync manager, Object result) {
txt.setText("下载完成");
}
public void onDownloadUpdate(DownloadManagerAsync manager, int percent) {
txt.setText("下载进度:" + String.valueOf(percent) + "%");
}
public void onDownloadError(DownloadManagerAsync manager, Exception e) {
txt.setText("下载出错");
}
}