Handler sendMessage 与 obtainMessage (sendToTarget)比较
2015-10-26 15:23
651 查看
转自:/article/3819221.html
obtainmessage()是从消息池中拿来一个msg 不需要另开辟空间new
new需要重新申请,效率低,obtianmessage可以循环利用;
[java] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
//use Handler.obtainMessage(),instead of msg = new Message();
//because if there is already an Message object,that not be used by
//any one ,the system will hand use that object,so you don't have to
//create and object and allocate memory.
//it is also another example of object recycling and reusing in android.
Message msg = mHandler.obtainMessage();
msg.what = UPDATE_LISTVIEW;
msg.obj = current + "/" + total + "songs";
//this method is called from worker Thread,so we cannot update UI from here.
msg.sendToTarget();
在看下面代码:
[java] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
Message msg = handler.obtainMessage();
msg.arg1 = i;
msg.sendToTarget();
Message msg=new Message();
msg.arg1=i;
handler.sendMessage(msg);
第一种写法是message 从handler 类获取,从而可以直接向该handler 对象发送消息,第二种写法是直接调用 handler 的发送消息方法发送消息。
obtainmessage()是从消息池中拿来一个msg 不需要另开辟空间new
new需要重新申请,效率低,obtianmessage可以循环利用;
[java] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
//use Handler.obtainMessage(),instead of msg = new Message();
//because if there is already an Message object,that not be used by
//any one ,the system will hand use that object,so you don't have to
//create and object and allocate memory.
//it is also another example of object recycling and reusing in android.
Message msg = mHandler.obtainMessage();
msg.what = UPDATE_LISTVIEW;
msg.obj = current + "/" + total + "songs";
//this method is called from worker Thread,so we cannot update UI from here.
msg.sendToTarget();
在看下面代码:
[java] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
Message msg = handler.obtainMessage();
msg.arg1 = i;
msg.sendToTarget();
Message msg=new Message();
msg.arg1=i;
handler.sendMessage(msg);
第一种写法是message 从handler 类获取,从而可以直接向该handler 对象发送消息,第二种写法是直接调用 handler 的发送消息方法发送消息。
相关文章推荐
- Claim 2
- POJ-3735-Training little cats-构造矩阵+矩阵快速幂+稀疏矩阵乘法优化
- RAID的后台初始化 Background Initialization
- Teradata Explain
- Timus Online Judge 1017 Staircases(记忆化搜索)
- ERROR ITMS-90049:"This bundle is invalid.The bundle identifier contains disallowed characters."
- 全局光照技术解析Global Illumination Explained
- TEMP_FAILURE_RETRY,__extension,MSG_NOSIGNAL
- Intellij IDEA无法启动:Failed to create JVM:error code -4
- Onpaint和OnDraw的区别
- aidl远程调用其他应用服务
- LPC43XX JTAG Scan Chain
- pthread_cond_timedwait 实例
- FairScheduler源码队列饥饿
- 【PAT】1086. Tree Traversals Again (25)
- FairScheduler源码计算fair share
- FairScheduler源码资源抢占
- FairScheduler源码任务分配
- Intel 82599 ixgbe & ixgbevf CNA 卡驱动分析02——VF/PF/MailBox
- CvSVM::EPS_SVR train_auto assertion sv_count != 0 failed原因