您的位置:首页 > 移动开发 > Android开发

我常用的BaseHandler

2016-07-11 14:27 531 查看
在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的作用:

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

实现方式很简单。下面是之前使用过的一个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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android开发