public interface Callback {
public boolean handleMessage(Message msg);
}
如果指定了则调用这个mCallBack的handleMessage函数,如果这个函数处理了逻辑返回true则整个处理完毕,如果返回false则调用Handler本身的handleMessage函数,这样的目的是我们可以不必要派生一个Handler类来实现handleMessage,而是指定一个外部的消息处理逻辑来处理消息。
public void dispatchMessage(Message msg) {
if (msg.callback != null) {
handleCallback(msg);
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg);
}
}
Handler h = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
// TODO Auto-generated method stub
Log.v("abc", "msgwhat:" + msg.what + msg.obj.getClass().getName());
return false;
}
});
Messenger mgr = new Messenger(h);
Intent intent = new Intent(YxbFirstTabActivity.this, FocusBankSelectActivity.class);
intent.putExtra("oybq", mgr);
startActivity(intent);
回调者:
Message msg=Message.obtain();
msg.obj=FocusBankSelectActivity.this;
try {
mgr.send(msg); //mgr从Intent中获得。
} catch (RemoteException e) {
e.printStackTrace();
}