Handler sendMessage 与 obtainMessage (sendToTarget)比较
2015-06-12 10:59
645 查看
转自:/article/3819221.html
obtainmessage()是从消息池中拿来一个msg 不需要另开辟空间new
new需要重新申请,效率低,obtianmessage可以循环利用;
[java] view plaincopyprint?
//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 plaincopyprint?
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 plaincopyprint?
//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();
//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 plaincopyprint?
Message msg = handler.obtainMessage();
msg.arg1 = i;
msg.sendToTarget();
Message msg=new Message();
msg.arg1=i;
handler.sendMessage(msg);
Message msg = handler.obtainMessage(); msg.arg1 = i; msg.sendToTarget(); Message msg=new Message(); msg.arg1=i; handler.sendMessage(msg);
第一种写法是message 从handler 类获取,从而可以直接向该handler 对象发送消息,第二种写法是直接调用 handler 的发送消息方法发送消息。
相关文章推荐
- Leetcode220-Contains Duplicate III
- Compiler Error Message: The compiler failed with error code 128. 的问题解决
- http://blog.csdn.net/name_Uncle_Wang/article/details/46438641?ref=myread
- 出现set_locale() failed (5): Input/output error的解决方法
- scanf问题
- Climbing Stairs爬楼梯——动态规划
- 9 Ways to Instantly Strengthen Your Brain
- [POJ 1273] Drainage Ditches & 最大流Dinic模板
- SceneRenderer_Main中的方法
- 记使用WaitGroup时的一个错误
- Training:组合博弈入门
- MediaStore.Images.Thumbnails
- AI 笔记(一)
- hdu 1848 Fibonacci again and again
- [case分享]Exchange 2010 登陆OWA查看邮件出现Rights managem operation failed
- [分享]Exchange企业邮箱被hotmail加入黑名单如何申诉
- main函数的参数
- Training:搜索入门
- Climbing Stairs
- 简述raid的种类和各种级别的特性