Android中Handler的sendEmptyMessage的理解

来源:互联网 发布:影响力的重要性知乎 编辑:程序博客网 时间:2024/05/20 22:01

在写代码的过程中,碰到一行代码不理解,去看了下源码,来记录下。

mHandler.sendEmptyMessage(0);
sendEmptyMessage这个方法之前没有碰到过,查阅源码后发现:

    /**     * Sends a Message containing only the what value.     *       * @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 sendEmptyMessage(int what)    {        return sendEmptyMessageDelayed(what, 0);    }

发现底层调用了sendEmptyMessageDelayed方法,

这个的解释是,只发送一个包含what值的message。

继续看源码:

    /**     * 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);    }

看到这里就知道了,确实发送了一个只包含what值的消息。


确实,不看源码的话,完全不知道这个方法到底做了什么,看来以后还是要多多阅读源码。




0 0