private void getData() {
        // TODO Auto-generated method stub
        
        
        new WaitProgress(new WaitProgress.WaitProgressCallBack() {
            
            @Override
            public void execute() {
                // TODO Auto-generated method stub
                TrainSearchOrderResponse response =PraseXML.getTrainOrderList(result);
                
                list=response.getTrainOrders();
                adapter=new TrainOrderListAdapter(TrainOrderListActivity.this, list);
                //adapter.GetSelectinfo(str_cc, str_cfz, str_ddz);
                listview=(ListView) findViewById(R.id.listview);
                Log.d("ssss", list.size()+"size11111111");
                listview.setAdapter(adapter);
                listview.setOnItemClickListener(new Onitemlistener());
                if(response!=null){
                    countview.setText(response.getTotalCount().toString());
                }
            }
        }, new WaitProgress.DataCallBack() {
            
            @Override
            public void execute() {
                RequestData r = new RequestDataImpl();
                TrainQueryOrderRequest request = new TrainQueryOrderRequest();
                Ve_yhb ve_yhb =  Ve_yhb.getVe_yhb();
                request.setStart("0");
                request.setCount("10");
                request.setKhid(ve_yhb.getId());
                result=r.searchTrainOrder(request.toXML());
            }
        }).waitDialog(TrainOrderListActivity.this);
        
    }

getdata是另外一个类里面进行数据请求时调用,一个程序有多个不同请求数据页面,那么execute里面执行的代码就是不一样的,但是线程处理的上是一样的,为了提高代码的共用性,那么线程处理部分代码是可以共用的部分,在waitprogress类里面写两个接口,方便在其他类(例如在getdata的那个类,需要进行请求数据),即在其他类可以完整实现你所写的那个类的方法。

package cn.vetech.android.framework.core.commons;




import cn.vetech.android.framework.core.commons.AlertViewDiaologInterface.DoCloseEvent;
import cn.vetech.android.framework.core.data.RequestData;
import cn.vetech.android.framework.core.httpclient.HttpUtils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

/**
 * 请求网络数据时的异步调用框架
 * @author 李盼
 *
 */
public class WaitProgress {
    BaseProgressDialog progressDialog;
    Handler handler;
    WaitProgressCallBack progressCallBack;
    DataCallBack dataCallBack; 
    DoCloseEvent closeEvent = null;
    
    RequestData data ;
    boolean isClosed = false;
    
    boolean checklogin = true;
    
    
    /**
     * 1,设置请求数据时的回调函数
     * 2,设置数据返回后处理数据的回调函数
     * @param progressCallBack
     * @param dataCallBack
     */
    public WaitProgress(WaitProgressCallBack progressCallBack,DataCallBack dataCallBack){
        this.progressCallBack = progressCallBack;
        this.dataCallBack = dataCallBack;
    }
    public WaitProgress(WaitProgressCallBack progressCallBack,DataCallBack dataCallBack,DoCloseEvent closeEvent){
        this.progressCallBack = progressCallBack;
        this.dataCallBack = dataCallBack;
        this.closeEvent = closeEvent;
    }
    public WaitProgress(WaitProgressCallBack progressCallBack,DataCallBack dataCallBack,boolean checklogin){
        this.progressCallBack = progressCallBack;
        this.dataCallBack = dataCallBack;
        this.checklogin = checklogin;
    }
    

    /**
     * 启动框架,弹出等待层,并开启线程进行网络数据访问
     * @param context
     */
    public void waitDialog(Context context,String message) {
        boolean isconnection = HttpUtils.isNetworkAvailable(context);
        
        
            if(isconnection){
    
                progressDialog = new ProgressDialog(context);
                
                //放回数据后进行数据处理,
                handler = new Handler() {
                    public void handleMessage(Message msg) {
                        progressCallBack.execute();
                    };
                };
                
                //开启线程进行网络访问,网络访问在dataCallBack.execute()回调函数中进型
                final Thread thread = new Thread() {
                    @Override
                    public void run() {  
                        dataCallBack.execute();
                        //返回数据后关闭弹出层
                        //向handler发送消息通知数据已经返回
                        if (!isClosed) {
                            Message msg = new Message();
                            handler.sendMessage(msg);
                        }
                        progressDialog.dismissDialog();
                    }
                };
    
                thread.start();
                
                //关闭弹出层的同时杀掉网络访问进程
                progressDialog.alertProgressDialog(message,new AlertViewDiaologInterface.StopNetWork() {
                    @Override
                    public void execute() {
                        isClosed = true;
                        thread.interrupt();
                    }
                });
                
                
            }else{
                DialogUtils.alert(context, "未找到可用的网络,请检查网络连接!");
            }
        
    }
    /**
     * 启动框架,弹出等待层,并开启线程进行网络数据访问
     * @param context
     */
    public void waitDialog(Context context) {
        boolean isconnection = HttpUtils.isNetworkAvailable(context);
        
            if(isconnection){
                if("1".equals(SysConfiguration.getTITLEFLAG())){
                    progressDialog = new ProgressDialog_BTLH(context);
                }else{
                    progressDialog = new ProgressDialog(context);
                }
                
                
                //放回数据后进行数据处理,
                handler = new Handler() {
                    public void handleMessage(Message msg) {
                        progressCallBack.execute();
                    };
                };
                
                //开启线程进行网络访问,网络访问在dataCallBack.execute()回调函数中进型
                final Thread thread = new Thread() {
                    @Override 
                    public void run() {  
                        dataCallBack.execute();
                        //返回数据后关闭弹出层
                        //向handler发送消息通知数据已经返回
                        if (!isClosed) {
                            Message msg = new Message();
                            handler.sendMessage(msg);
                        }
                        progressDialog.dismissDialog();
                    }
                };
                
                thread.start();
                
                //关闭弹出层的同时杀掉网络访问进程nbvc`13rty\
                
                progressDialog.alertProgressDialog("",new AlertViewDiaologInterface.StopNetWork() {
                    @Override
                    public void execute() {
                        isClosed = true;
                        thread.interrupt();
                    }
                },closeEvent);
                
                
                
            }else{
                DialogUtils.alert(context, "未找到可用的网络,请检查网络连接!");
            }
    }

    

    /**
     * 处理返回数据的回调函数
     *
     */
    public interface WaitProgressCallBack {
        public void execute( );
    }
    /**
     * 请求数据回调函数
     *
     */
    public interface DataCallBack {
        public void execute( );
    }
}

 

posted on 2014-04-02 13:09  青年程序猿  阅读(437)  评论(0编辑  收藏  举报