I am trying to implement Handlers listening on the same Looper from different threads.
Below I have two Handlers, one created in the main thread, another in the child thread, however both are initialized to listen on the Main Looper.
private Handler mMain;
public static final ThreadPoolExecutor tpe =
(ThreadPoolExecutor) Executors.newCachedThreadPool();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMain = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
Log.wtf("", "main:" + msg);
}
};
tpe.execute(new Runnable() {
private Handler tChild = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
Log.wtf("", "child:" + msg);
}
};
@Override
public void run() {
Log.wtf("", "send msg to main looper");
tChild.sendEmptyMessage(100);
}
});
}
But when I send a message like below, only the child handler prints the message. The main handler does not receive the message.
03-20 22:02:26.754: A/(12857): send msg to main looper
03-20 22:02:26.847: A/(12857): child:{ what=100 when=-8ms }
What am I doing wrong? Thank you for reading.