广播的方法很方便、但是一堆的消息接收、发送很模式化
2、对于较多的方法调用 个人建议直接写共用类中
此方法通常用于数据处理、对接复杂界面处理颇感无力
3、结合父类、根据实际情况调用
今天介绍的就是通过接口调用 选择获取消息数量方法
首先介绍下主逻辑、即在子类调用父类方法、并将子类handler传入父类、进行数据返回
--------------------------------------分割线--------------------------------------
想下载DEMO的可以到Linux公社资源下载
具体下载目录在 /2013年资料/12月/9日/Android 调用父类方法,进行子界面刷新
--------------------------------------分割线--------------------------------------
子类主要代码:
protected void onResume() {
// TODO Auto-generated method stub
if (app.jkNeedReadNum)
getNoReadNum(app.userid, handler);
super.onResume();
}
private Handler handler = new Handler() {
/*
* (non-Javadoc)
*
* @see android.os.Handler#handleMessage(android.os.Message)
*/
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case GET_NUM_SUCCESS:
String count = (String) msg.obj;
badge1.setText(count);
badge1.show();
break;
case GET_NUM_CANCLE:
badge1.hide();
break;
}
}
};
父类主要代码:
// 获取未阅读数量
public void getNoReadNum(long user_id, Handler handler) {
RequestParams params = new RequestParams();
params.put("user_id", user_id + "");
HttpConnection.mesNoreadSum(params, new NumAsyncHttpResponseHandler(
handler));
}
class NumAsyncHttpResponseHandler extends AsyncHttpResponseHandler {
Handler handler;
public NumAsyncHttpResponseHandler(Handler handler) {
this.handler = handler;
}
@Override
public void onStart() {
// TODO Auto-generated method stub
app.jkNeedReadNum = false;
super.onStart();
}
/*
* (non-Javadoc)
*
* @see com.emotte.result.AsyncHttpResponseHandler#onFinish()
*/
@Override
public void onFinish() {
// TODO Auto-generated method stub
app.jkNeedReadNum = true;
super.onFinish();
}
/*
* (non-Javadoc)
*
* @see
* com.emotte.result.AsyncHttpResponseHandler#onSuccess(java.lang.String
* )
*/
@Override
public void onSuccess(String content) {
// TODO Auto-generated method stub
app.jkNeedReadNum = true;
try {
// JSONObject obj = new JSONObject(content);
// String result = obj.optString("success");
// if (result != null || "0".equals(result)) {
// String count = obj.optString("count");
//
String count = "22";
System.out.println(count);
if (count != null && Integer.parseInt(count) > 0) {
Message m = handler.obtainMessage();
m.what = GET_NUM_SUCCESS;
m.obj = count;
handler.sendMessage(m);
} else {
Message m = handler.obtainMessage();
m.what = GET_NUM_CANCLE;
handler.sendMessage(m);
}
// }
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.onSuccess(content);
}
/*
* (non-Javadoc)
*
* @see
* com.emotte.result.AsyncHttpResponseHandler#onFailure(java.lang.Throwable
* , java.lang.String)
*/
@Override
public void onFailure(Throwable error, String content) {
// TODO Auto-generated method stub
app.jkNeedReadNum = true;
super.onFailure(error, content);
}
};
为了不重复调用、此处定义了全局变量用于判断。