The second method can judge the sent message and perform different processing. If there is no need for classification processing, sending an empty message is enough.
Looking from the source code First, sendEmptyMessage(int what)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);
}
/**
* 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()
rrreee
Then click sendEmptyMessageDelayed(what, 0)
rrreee
From the sendEmptyMessageDelayed(int what, long delayMillis) method you can see that the what parameter is assigned to the Message object obtained
Look at the source code of obtainMessage(1,data)#🎜🎜#
rrreee
#🎜🎜#The method returns Message.obtain(this, what, obj), then continue to look at obtain(this, what, obj)#🎜🎜#
rrreee
#🎜🎜#The Message in the method is also obtained through obtain, but here the obj object is also assigned to the Message. #🎜🎜#The target in the source code of sendToTarget() is Handler, which means the sendMessage() method is called. #🎜🎜#
rrreee
rrreee
#🎜🎜#sendEmptyMessage(int what) and obtainMessage(int what, Object obj).sendToTarget()#🎜🎜#The biggest difference is that there is no Object object# 🎜🎜#
#🎜🎜#Reference: #🎜🎜#What is the difference between Handler’s sendEmptyMessage(int what) and sendMessage(Message msg)? #🎜🎜#Comparison of Handler sendMessage and obtainMessage (sendToTarget)#🎜🎜#
The second method can judge the sent message and perform different processing. If there is no need for classification processing, sending an empty message is enough.
Looking from the source code
First,
sendEmptyMessage(int what)
sendEmptyMessage(int what)
再点进
sendEmptyMessageDelayed(what, 0)
从
sendEmptyMessageDelayed(int what, long delayMillis)
方法中可以看到what参数赋给了obtain出来的Message对象
再从
obtainMessage(1,data)
的源码中看方法返回
Message.obtain(this, what, obj)
,接着继续看obtain(this, what, obj)
方法里面Message也是通过obtain来获取的,不过这里还将obj对象赋给了Message。
sendToTarget()
的源码中的target是Handler,就是调用了sendMessage()
方法
rrreeesendEmptyMessage(int what)
和obtainMessage(int what, Object obj).sendToTarget()
rrreeeThen click
sendEmptyMessageDelayed(what, 0)
From the
sendEmptyMessageDelayed(int what, long delayMillis)
methodyou can see that the what parameter is assigned to the Message object obtained
Look at the source code of
obtainMessage(1,data)
#🎜🎜# rrreee #🎜🎜#The method returnsMessage.obtain(this, what, obj)
, then continue to look atobtain(this, what, obj)
#🎜🎜# rrreee #🎜🎜#The Message in the method is also obtained through obtain, but here the obj object is also assigned to the Message. #🎜🎜#The target in the source code ofsendToTarget()
is Handler, which means thesendMessage()
method is called. #🎜🎜# rrreee rrreee#🎜🎜#
sendEmptyMessage(int what)
andobtainMessage(int what, Object obj).sendToTarget()
#🎜🎜#The biggest difference is that there is no Object object# 🎜🎜# #🎜🎜#Reference: #🎜🎜#What is the difference between Handler’s sendEmptyMessage(int what) and sendMessage(Message msg)? #🎜🎜#Comparison of Handler sendMessage and obtainMessage (sendToTarget)#🎜🎜#There is not much difference. The first one is to send an empty message, and the second one is to take a message from the message pool and send it.
It’s no different, it just provides a variety of APIs for you to use, and you can use them according to your habits