您的位置:首页 > 其它

sendMessage和sendEmptyMessage的区别

2017-06-09 14:54 483 查看
看源码 根据参数的不同互相调用
public final boolean sendMessage(Message msg)
{
return sendMessageDelayed(msg, 0);
}

/**
* 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);
}
//
/*** 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()而已,下面看下这个方法:/*** 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);}而sendMessage(Message msg)的实现和上面一样,请看:/*** Pushes a message onto the end of the message queue after all pending messages* before the current time. It will be received in {@link #handleMessage},* in the thread attached to this handler.* * @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 sendMessage(Message msg){return sendMessageDelayed(msg, 0);}原来在sendEmptyMessageDelayed中就是构建了一个Message,然后把这个Message的what设置成sendEmptyMessage方法中的What参数即可。sendMessage()允许你处理Message对象(Message里可以包含数据,)。sendEmptyMessage()只能放数据 ,并且是从消息池获取的message, 性能上比sendMessage快. 只是不能操作这个对象
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐