1.实例化
TableListsTask task = new TableListsTask(ServerIP,"ALL", MenuActivity.this);
//第三参数建立上下文关系
2.TableListsTask.java
package com.realhope.rmeal.ui; import static com.realhope.rmeal.service.ConstantUtil.SERVER_ADDRESS; import static com.realhope.rmeal.service.ConstantUtil.SERVER_PORT; import android.content.Context; import android.os.AsyncTask; import android.util.Log; import com.realhope.rmeal.bean.TableLists; import com.realhope.rmeal.util.MyUtil; /**异步任务 方法:menuactivity 获取台列表 * 2014-7-31 * @author Wucy * */ public class TableListsTask extends AsyncTask<String, Integer, String> { //onPreExecute方法用于在执行后台任务前做一些UI操作 private static final String TAG = "ASYNC_TableListsTASK"; private MyConnector mc = null; private final Context mContext; String mtable_no,mServerIP; public TableListsTask(String ServerIP,String table_no, Context context){ super(); mContext = context; mtable_no =table_no; mServerIP=ServerIP; } @Override protected void onPreExecute() { Log.i(TAG, "onPreExecute() called"); // MyUtil.showToast(OrderActivity.this, "数据上传..."); // textView.setText("loading..."); //button_toOrder.setText("正在查询...."); } //doInBackground方法内部执行后台任务,不可在此方法内修改UI @Override protected String doInBackground(String... params) { Log.i(TAG, "doInBackground(Params... params) called"); try{ if(mc == null){ mc = new MyConnector(mServerIP, SERVER_PORT); } String msg = "<#GET_TABLELIST#>"+ mtable_no; mc.dout.writeUTF(msg); //发出获取台表请求 mc.dout.flush(); int size = mc.din.readInt(); //获取个数 mc.dout.writeUTF("<#READY_TO_READ_COMMENT#>"); mc.dout.flush(); for(int i=0;i<size;i++){ msg = mc.din.readUTF(); //读取每条信息 String [] sa = msg.split("\\|"); //切割字符串 TableLists tablelists = new TableLists(Integer.valueOf(sa[0]),Integer.valueOf(sa[1]),sa[2],Integer.valueOf(sa[3]),Integer.valueOf(sa[4]),Integer.valueOf(sa[5])); ((MenuActivity)mContext).lstDate_TableLists.add(tablelists); } mc=null; return String.valueOf(1); } catch (Exception e) { Log.e(TAG, e.getMessage()); } return null; } //onProgressUpdate方法用于更新进度信息 @Override protected void onProgressUpdate(Integer... progresses) { Log.i(TAG, "onProgressUpdate(Progress... progresses) called"); } //onPostExecute方法用于在执行完后台任务后更新UI,根据后台下单结果重新绑定前台订单 @Override protected void onPostExecute(String result) { Log.i(TAG, "onPostExecute(Result result) called"); MyUtil.showToast(mContext, "取后台开台信息数据"); ((MenuActivity)mContext).updateTableLists(); } //onCancelled方法用于在取消执行中的任务时更改UI @Override protected void onCancelled() { Log.i(TAG, "onCancelled() called"); } }