search

Home  >  Q&A  >  body text

android - Handler dispatchMessage

额,有个疑问,求大神赐教。
MyHandler继承自Handler:

    class MyHandler extends Handler {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            Log.e(TAG, "disaptch message");
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.e(TAG, "handle message");
        }
    }

然后写了一个Message,设置了它的callback,然后把这个message发送出去。

        mMyHandler = new MyHandler();
        Message msg = Message.obtain(mMyHandler, new Runnable() {
            @Override
            public void run() {
                Log.e(TAG, "Message Callback");
            }
        });
        mMyHandler.sendMessage(msg);

问题是,既然MyHandler的dispatchMessage方法被我复写了,那么在Looper调用MyHandler.dispatchMessge
方法时,应该不会去调Message的callback,那么Log.e(TAG, "Message Callback");应该就不会执行,但为什么还是会打印出来?

怪我咯怪我咯2771 days ago744

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 17:27:18

    Is the super.dispatchMessage(msg); method not executed? You overridden it, but the original method is still executed. Why is it not executed?

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 17:27:18

    super.dispatchMessage(msg) source code is to first determine if there is an incorrect callback interface, and if so, execute the callback.

    reply
    0
  • Cancelreply