I’ve created a custom handler that will take care of dismissing dialogs, showing dialogs and showing (custom) error messages. However, I’m stuck with the custom error message part. How do I post a message with custom message & how to parse it in the handleMessage?
Now I’m doing:
handler.sendMessage(Message.obtain(handler, HANDLER_MESSAGE_ERROR));
I’ve read about bundle, but didn’t get it working. Preferably (to keep the code neat), I’d like to do something like this:
handler.sendMessage(Message.obtain(handler, HANDLER_MESSAGE_ERROR, "Custom error message"));
and for error dialog:
handler.sendMessage(Message.obtain(handler, HANDLER_MESSAGE_DIALOG, "Custom title", "Custom error message"));
Here’s the handler code that I’m using right now:
public class MyHandler extends Handler {
private Activity mContext;
public MyHandler(Activity activity) {
mContext = activity;
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MyActivity.HANDLER_MESSAGE_ERROR:
try {
Toast.makeText(mContext, "_This should be a custom error message", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
}
break;
}
}
}
I have used handler to pass different messges. This code will work for you.
Pass only simple message:
Pass title and message:
You will get the bundle in your method in the following way