概覽
l 目的
l 實現
l 注意事項
目的
在手機客戶端與服務器交互時,如果訪問的數據量過大難免會出現等待時間,這期間引入ProgressDialog或其他加載進度顯示界面將會是一個很友好的選擇。通常我們選擇android Handler消息機制解決ProgressDialog顯示的問題。但是當我們從一個Activity跳到另一個Activity之間也有很大的數據加載等待,這種情況下使用AsyncTask將會是一個很很好的選擇。本文將會以Handler Message機制和AsyncTask實現android異步數據處理。
實現
l Handler Message 實現
原理:
1 用戶觸發Button或其他數據加載事件
2 產生數據加載,發送消息,通知加載對話框
3 這期間即初始化加載對話框
4 數據記載完成,發送消息通知關閉加載對話框
1)繼承Handler的MyHandler類:
private class MyHandle extends Handler {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case 0:
// 發送關閉對話框消息
Message close = new Message();
close.what = 1;
mHandler.sendMessage(close);
break;
case 1:
if (pDialog != null) {
pDialog.dismiss();
}
break;
default:
break;
}
}
}
2)加載對話框:
/**
* @descript 初始化進度條對話框
* @param
* @rvoid
*/
private void initPDialog() {
// TODO Auto-generated method stub
pDialog = new ProgressDialog(mContext);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setMessage("數據獲取中。。。");
pDialog.show();
}
3)實例化Handler:
// 實例化Handler
mHandler = new MyHandle();
4)打開加載對話框消息發送
// 發送加載對話框消息
Message open = new Message();
open.what = 0;
mHandler.sendMessage(open);
l AsyncTask實現
1 定義繼承自AsyncTask的GetDataAsyncTask類
public class GetDataAsyncTask extends AsyncTask<int[], Integer, int[]> {
public GetColorAsyncTask(Context context) {
initDialog();// 初始化進度對話框
}
/*
* (non-Javadoc)
*
* @see android.os.AsyncTask#doInBackground(Params[])
*/
@Override
protected int[] doInBackground(int[]... params) {
// TODO Auto-generated method stub
getData();// 耗時操作,AsyncTask處理你的數據邏輯
return data;
}
/*
* (non-Javadoc)
*
* @see android.os.AsyncTask#onCancelled()
*/
@Override
protected void onCancelled() {
// TODO Auto-generated method stub
super.onCancelled();
}
/*
* (non-Javadoc)
*
* @see android.os.AsyncTask#onPostExecute(java.lang.Object)
*/
@Override
protected void onPostExecute(int[] result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
// 更新UI
//… your code here
//…
// 關閉進度窗口
if (pDialog != null) {
pDialog.dismiss();
}
}
/*
* (non-Javadoc)
*
* @see android.os.AsyncTask#onPreExecute()
*/
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
/*
* (non-Javadoc)
*
* @see android.os.AsyncTask#onProgressUpdate(Progress[])
*/
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
}
2) 初始化加載對話框
/**
* @descript 初始化進度條對話框
* @param
* @rvoid
*/
private void initDialog() {
// TODO Auto-generated method stub
pDialog = new ProgressDialog(mContext);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setMessage("數據獲取中。。。");
pDialog.show();
}
3)在Activity的主UI線程中實例化你的AsyncTask
// AsyncTask code
GetDataAsyncTask task = new GetDataAsyncTask(mContext);
task.execute(data);
注意事項
l 關閉與打開對話框語句不要寫在Handler提交的自定義線程內,不然會出現對話框不關閉的情形。
l 對於AsyncTask實現中,doInBackground()方法中不能訪問UI和更新UI。數據的訪問與加載都在這個方法中實現;如果需要訪問和更新UI需要在提交Result的onPostExecute()中實現;(the doInBackground is run in the background, and while you are inthe backgroud, you CAN NOT touch the UI. AdoInBackground operation should return a result, and that result will be passedto the onPostExecute. The onPostExecute is run on the UI thread and it is herethat you can update any UI elements, which includes setting a new list adapater.)。