我常用的BaseHandler

Android开发过程中,我们不免会使用Handler类用来处理多线程发来的消息。

一般使用过程中,我们在Activity中定义MyHandler extends Hanlder,并重写了handleMessage方法,在多线程的地方通过sendMessage的方式发送消息,在handlerMessage里使用

</pre><pre name="code" class="java">switch(msg.what)
{ 
case 1:break; 
case2:break;
}
的方式进行分发处理。

码了几个MyHandler你会发现,他们基本上都会处理“请求成功”,“请求失败”,“发送通知”等相关事务。所以为了减少冗余代码,在MyHandler之上定义一个BaseHandler是很有必要的。

BaseHandler的作用:

  1. 封装全局适用的消息msg.whatKEY),例如:定义发送通知的KEY = 1000
  2. 处理全局适用的消息。 例如:可以在BaseHandler里处理发送通知的消息。
  3. 定义对所有Handler适用的方法。
  4. ...

实现方式很简单。下面是之前使用过的一个BaseHandler,因为大量使用了SwipeRefreshLayout,所以我把该控件的状态更新也放到了BaseHandler中进行处理。

public class BaseHandler extends Handler {
    private static final String TAG = "BaseHandler";
    protected Context context;
    protected SwipeRefreshLayout mSwipeRefreshLayout;

    protected Snackbar snackbar;

    public BaseHandler(Context context, SwipeRefreshLayout mSwipeRefreshLayout){
        this.context = context;
        this.mSwipeRefreshLayout = mSwipeRefreshLayout;
    }

    public static final int KEY_TOAST = 1000;
    public static final int KEY_TOAST_LONG = 1001;
    public static final int KEY_DISSMISS_PROGRESS = 1002;
    public static final int KEY_NEED_LOGGED = 1003;
    public static final int KEY_ERROR = 2001;
    public static final int KEY_SUCCESS = 2000;
    public static final int KEY_NO_RES = 2002;
    public static final int KEY_PARSE_ERROR = 2003;

    /**
     * 获取七牛token成功是发送该消息
     */
    public static final int KEY_GET_QINIU_TOKEN_SUC = 3001;
    /**
     * 给trend 点赞
     */
    public static final int KEY_LIKE_TREND = 3002;
    /**
     * 给trend 取消点赞
     */
    public static final int KEY_UNLIKE_TREND = 3003;
    /**
     * 获取用户的trend
     */
    public static final int KEY_GET_TREND_LIST_SUC = 3004;
    /**
     * 获取用户粉丝
     */
    public static final int KEY_GET_FENSI_SUC = 3005;
    /**
     * 获取用户关注
     */
    public static final int KEY_GET_GUANZHU_SUC = 3006;
    /**
     * 获取用户简要信息
     */
    public static final int KEY_GET_BRIEFUSER_SUC = 3007;
    /**
     * 获取gym列表成功
     */
    public static final int KEY_GET_GYM_LIST_SUC = 3008;
    /**
     * 获取用户简要信息
     */
    public static final int KEY_GET_RECOMMENDUSER_SUC = 3009;

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);

        Log.d(TAG, "BaseHandler开始及处理");

        switch (msg.what){
            case KEY_ERROR:
                if (context != null){
                    Toast.makeText(context, (String) msg.obj, Toast.LENGTH_SHORT).show();
                }
                if (mSwipeRefreshLayout != null){
                    mSwipeRefreshLayout.setRefreshing(false);
                }
                break;
            case KEY_PARSE_ERROR:
                if (context != null){
                    Toast.makeText(context, "数据出错了,再试试", Toast.LENGTH_SHORT).show();
                }
                if (mSwipeRefreshLayout != null){
                    mSwipeRefreshLayout.setRefreshing(false);
                }
                break;
            case KEY_NO_RES:
                if (context != null){
                    Toast.makeText(context, "无法连接到服务器", Toast.LENGTH_SHORT).show();
                }
                if (mSwipeRefreshLayout != null){
                    mSwipeRefreshLayout.setRefreshing(false);
                }
                break;
            case KEY_TOAST:
                if (context != null){
                    if(snackbar == null) {
                        snackbar = Snackbar.make(((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content)
                                , (String) msg.obj
                                , Snackbar.LENGTH_SHORT);
                        snackbar.show();
                    } else{
                        snackbar.setText((String) msg.obj);
                        snackbar.show();
                    }
                }
                break;
            case KEY_TOAST_LONG:
                if (context != null){
                    Snackbar.make(((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content)
                            , (String) msg.obj
                            , Snackbar.LENGTH_LONG).show();
                }
                break;
            case KEY_DISSMISS_PROGRESS:
                try {
                    ((BaseAppCompatActivity) context).dissmissProgress();
                } catch(Exception e){

                }
                break;

            case KEY_NEED_LOGGED:
                TwoOptionMaterialDialog md_login_register = MaterialDialogFactory.createLoginOrRegisterMd(context);
                md_login_register.show();
                break;
        }
    }

    public void sendToast(String content){
        Message msg = Message.obtain();
        msg.what = KEY_TOAST;
        msg.obj = content;
        this.sendMessage(msg);
    }

    public void sendToastLong(String content){
        Message msg = Message.obtain();
        msg.what = KEY_TOAST_LONG;
        msg.obj = content;
        this.sendMessage(msg);
    }

    public void sendDisMissProgress(){
        Message msg = Message.obtain();
        msg.what = KEY_DISSMISS_PROGRESS;
        this.sendMessage(msg);
    }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值