You need not extend all Handler
class if you just want to handle some messages, just implement Handler.Callback
:
private final class UICallback implements Handler.Callback{
public static final int DISPLAY_UI_TOAST = 0;
private static final int LOAD_PROFILE = 1;
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case UICallback.DISPLAY_UI_TOAST: {
Context context = mw.getApplicationContext();
Toast t = Toast.makeText(context, (String) msg.obj,
Toast.LENGTH_SHORT);
t.show();
return true;
}
case UICallback.LOAD_PROFILE:{
loadProfile((String) msg.obj);
return true;
}
default:
return false;
}
}
}
Note, in above code, true is returned when message is handled, false when message was not meant for us.
Now, create a handler with main looper (so that all messages run on UI thread ), and the callback we created:
Handler handler = new Handler(Looper.getMainLooper(),new UICallback());