Handler Message 消息机制和AsyncTask异步处理android数据交互

来源:互联网 发布:编写网页的软件 编辑:程序博客网 时间:2024/04/29 12:24

概览

l  目的

l  实现

l  注意事项

 

目的

在手机客户端与服务器交互时,如果访问的数据量过大难免会出现等待时间,这期间引入ProgressDialog或其他加载进度显示界面将会是一个很友好的选择。通常我们选择android Handler消息机制解决ProgressDialog显示的问题。但是当我们从一个Activity跳到另一个Activity之间也有很大的数据加载等待,这种情况下使用AsyncTask将会是一个很很好的选择。本文将会以Handler Message机制和AsyncTask实现android异步数据处理。

实现       

l  Handler Message 实现

 

原理:

1 用户触发Button或其他数据加载事件

2 产生数据加载,发送消息,通知加载对话框

3 这期间即初始化加载对话框

4 数据记载完成,发送消息通知关闭加载对话框

 

1)继承HandlerMyHandler类:

private class MyHandle extends Handler {@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubsuper.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 stubpDialog = new ProgressDialog(mContext);pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);pDialog.setMessage("数据获取中。。。");pDialog.show();}


 

3)实例化Handler

// 实例化HandlermHandler = new MyHandle();


 

4)打开加载对话框消息发送

 

// 发送加载对话框消息Message open = new Message();open.what = 0;mHandler.sendMessage(open);


l  AsyncTask实现

 

 

1 定义继承自AsyncTaskGetDataAsyncTask

public class GetDataAsyncTask extends AsyncTask<int[], Integer, int[]> {public GetColorAsyncTask(Context context) {initDialog();// 初始化进度对话框}/* * (non-Javadoc) *  * @see android.os.AsyncTask#doInBackground(Params[]) */@Overrideprotected int[] doInBackground(int[]... params) {// TODO Auto-generated method stubgetData();// 耗时操作,AsyncTask处理你的数据逻辑return data;}/* * (non-Javadoc) *  * @see android.os.AsyncTask#onCancelled() */@Overrideprotected void onCancelled() {// TODO Auto-generated method stubsuper.onCancelled();}/* * (non-Javadoc) *  * @see android.os.AsyncTask#onPostExecute(java.lang.Object) */@Overrideprotected void onPostExecute(int[] result) {// TODO Auto-generated method stubsuper.onPostExecute(result);// 更新UI      //…  your code here         //…// 关闭进度窗口if (pDialog != null) {pDialog.dismiss();}}/* * (non-Javadoc) *  * @see android.os.AsyncTask#onPreExecute() */@Overrideprotected void onPreExecute() {// TODO Auto-generated method stubsuper.onPreExecute();}/* * (non-Javadoc) *  * @see android.os.AsyncTask#onProgressUpdate(Progress[]) */@Overrideprotected void onProgressUpdate(Integer... values) {// TODO Auto-generated method stubsuper.onProgressUpdate(values);}}


2) 初始化加载对话框

/** * @descript 初始化进度条对话框 * @param * @rvoid */private void initDialog() {// TODO Auto-generated method stubpDialog = new ProgressDialog(mContext);pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);pDialog.setMessage("数据获取中。。。");pDialog.show();}3)在Activity的主UI线程中实例化你的AsyncTask
 
// AsyncTask codeGetDataAsyncTask task = new GetDataAsyncTask(mContext);task.execute(data);


 

注意事项

l  关闭与打开对话框语句不要写在Handler提交的自定义线程内,不然会出现对话框不关闭的情形。

l  对于AsyncTask实现中,doInBackground()方法中不能访问UI和更新UI。数据的访问与加载都在这个方法中实现;如果需要访问和更新UI需要在提交ResultonPostExecute()中实现;(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.)。

 

 

原创粉丝点击