Android的HTTP通信 XML解析和异步消息处理
2010-02-03 19:10:00 来源:WEB开发网// 发送消息
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;
更多精彩
赞助商链接