cari

Rumah  >  Soal Jawab  >  teks badan

android - 菜鸟对于 handler发送msg的疑问。

handler.sendEmpityMessage();和handler.obtainMessage(1,data).sendToTarget();有什么不同

伊谢尔伦伊谢尔伦2781 hari yang lalu939

membalas semua(4)saya akan balas

  • ringa_lee

    ringa_lee2017-04-17 17:22:17

    第二个方法可以对所发送的消息进行判断,从而进行不同的处理。如果没有分类处理的需求,发一个空消息就够了。

    balas
    0
  • 天蓬老师

    天蓬老师2017-04-17 17:22:17

    从源码看
    先是sendEmptyMessage(int what)

        public final boolean sendEmptyMessage(int what)
        {
            return sendEmptyMessageDelayed(what, 0);
        }

    再点进sendEmptyMessageDelayed(what, 0)

        /**
         * Sends a Message containing only the what value, to be delivered
         * after the specified amount of time elapses.
         * @see #sendMessageDelayed(android.os.Message, long) 
         * 
         * @return Returns true if the message was successfully placed in to the 
         *         message queue.  Returns false on failure, usually because the
         *         looper processing the message queue is exiting.
         */
        public final boolean sendEmptyMessageDelayed(int what, long delayMillis) {
            Message msg = Message.obtain();
            msg.what = what;
            return sendMessageDelayed(msg, delayMillis);
        }

    sendEmptyMessageDelayed(int what, long delayMillis)方法中
    可以看到what参数赋给了obtain出来的Message对象


    再从obtainMessage(1,data)的源码中看

        /**
         * 
         * Same as {@link #obtainMessage()}, except that it also sets the what and obj members 
         * of the returned Message.
         * 
         * @param what Value to assign to the returned Message.what field.
         * @param obj Value to assign to the returned Message.obj field.
         * @return A Message from the global message pool.
         */
        public final Message obtainMessage(int what, Object obj)
        {
            return Message.obtain(this, what, obj);
        }

    方法返回Message.obtain(this, what, obj),接着继续看obtain(this, what, obj)

        public static Message obtain(Handler h, int what, Object obj) {
            Message m = obtain();
            m.target = h;
            m.what = what;
            m.obj = obj;
    
            return m;
        }

    方法里面Message也是通过obtain来获取的,不过这里还将obj对象赋给了Message。
    sendToTarget()的源码中的target是Handler,就是调用了sendMessage()方法

        /**
         * Sends this Message to the Handler specified by {@link #getTarget}.
         * Throws a null pointer exception if this field has not been set.
         */
        public void sendToTarget() {
            target.sendMessage(this);
        }
        public final boolean sendMessage(Message msg)
        {
            return sendMessageDelayed(msg, 0);
        }

    sendEmptyMessage(int what)obtainMessage(int what, Object obj).sendToTarget()
    最大的不同就是没有了Object这个对象

    参考:
    Handler 的sendEmptyMessage(int what)和sendMessage(Message msg)有啥区别?
    Handler sendMessage 与 obtainMessage (sendToTarget)比较

    balas
    0
  • 天蓬老师

    天蓬老师2017-04-17 17:22:17

    没啥太大不同,第一个是发送空消息,第二个是从消息池取一个消息发送。

    balas
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 17:22:17

    没什么不同,只是提供了多样的API给你用,你怎么用的习惯就怎么用

    balas
    0
  • Batalbalas