【Android消息处理机制】Handler中sendEmptyMessage与sendMessage的区别和sendMessageAtTime()与sendEmptyMessageDelay()区别

本文转载自:http://blog.csdn.net/cangchen/article/details/44489023


1、sendEmptyMessage与sendMessage的区别:
sendMessage()允许你处理Message对象(Message里可以包含数据,)。
sendEmptyMessage(int what)只能放数据。其中参数what作用:就类似于ID,处理消息的时候用于区分你send一个0和1,处理的时候就要判断了if(msg.what == 0){}else if(msg.what == 1){}。

其实两者没区别,请看下面Handler的源代码:

 /**

* 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()而已,下面看下这个方法:

[java] view plain copy
  1. /** 
  2. * Sends a Message containing only the what value, to be delivered 
  3. * after the specified amount of time elapses. 
  4. * @see #sendMessageDelayed(android.os.Message, long)  
  5.  
  6. * @return Returns true if the message was successfully placed in to the  
  7. * message queue. Returns false on failure, usually because the 
  8. * looper processing the message queue is exiting. 
  9. */  
  10. public final boolean sendEmptyMessageDelayed(int what, long delayMillis) {  
  11. Message msg = Message.obtain();  
  12. msg.what = what;  
  13. return sendMessageDelayed(msg, delayMillis);  
  14. }  
而sendMessage(Message msg)的实现和上面一样,请看:

[java] view plain copy
  1. /** 
  2. * Pushes a message onto the end of the message queue after all pending messages 
  3. * before the current time. It will be received in {@link #handleMessage}, 
  4. * in the thread attached to this handler. 
  5.  
  6. * @return Returns true if the message was successfully placed in to the  
  7. * message queue. Returns false on failure, usually because the 
  8. * looper processing the message queue is exiting. 
  9. */  
  10. public final boolean sendMessage(Message msg)  
  11. {  
  12. return sendMessageDelayed(msg, 0);  
  13. }  
原来在sendEmptyMessageDelayed中就是构建了一个Message,然后把这个Message的what设置成sendEmptyMessage方法中的What参数即可。

  一切恍然大悟!

  然后,在主线程中,Looper类的loop()通过 调用: msg.target.dispatchMessage(msg),调用Hanler类的dispatchMessage(Message msg)方法,从而在主线程中处理了这个Message.

2、sendMessageAtTime()与sendEmptyMessageDelay()的区别:

 
 
函数原型:
 
public boolean sendMessageAtTime (Message msg, long uptimeMillis)
 
Message  //不用说 是待发送消息
uptimeMillis   //sendMessageAtTime,即在确定的时间发送这个消息,这个时间通过这个参数指定
这个时间由uptimeMillis()传递
  
这两句是等效的,都是延时1秒将消息加入列队
msgHandle.sendMessageAtTime(msg, SystemClock.uptimeMillis()+1000);
msgHandle.sendMessageDelayed(msg, 1000)
 
sendMessageAtTime的uptimeMillis是相对系统开机时间的绝对时间,SystemClock.uptimeMillis()是当前开机时间。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值