Handler sendMessage 与 obtainMessage (sendToTarget)比较
转自:http://iaiai.iteye.com/blog/1992196
obtainmessage()是从消息池中拿来一个msg 不需要另开辟空间new
new需要重新申请,效率低,obtianmessage可以循环利用;
- //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();
在看下面代码:
- 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 的发送消息方法发送消息。
Handler sendMessage 与 obtainMessage (sendToTarget)比较的更多相关文章
- Handler sendMessage 与 obtainMessage (sendToTarget)
这篇文章讲的很好: http://www.cnblogs.com/android007/archive/2012/05/10/2494766.html 两种用法: 1. private void se ...
- Android sendMessage 与 obtainMessage (sendToTarget)比较
话说在工作中第一次接触android 的Handler 的时候,不知道怎么去关注性能. 记得当时这么写的: Message msg = new Message() msg.what = xxx; ms ...
- sendMessage 与 obtainMessage (sendToTarget)比较
我们平时在做到多线程问题的时候可能利用Handler去传递Message,其中,经常使用的就是 1.new Handler().obtainMessage().sendToTarget(); 2.ne ...
- Handler.sendMessage 与 Handler.obtainMessage.sendToTarget比较
原文地址: http://www.cnblogs.com/android007/archive/2012/05/10/2494766.html 话说在工作中第一次接触android 的Handler ...
- 91、sendToTarget与sendMessage
Message msg = handler.obtainMessage(); msg.arg1 = i; msg.sendToTarget(); ...
- Android handler.obtainMessage()
在handler.obtainMessage()的参数是这样写的: Message android.os.Handler.obtainMessage(int what, int arg1, int a ...
- 【转】Handler学习笔记(二)
一.一个问题 有这样一个问题值得我们思考,若把一些类似于下载的功能(既耗时且不一定有结果)写在Activity(主线程)里,会导致Activity阻塞,长时间无响应,直至页面假死(如果5秒钟还没有完成 ...
- Android--多线程之Handler
前言 Android的消息传递机制是另外一种形式的“事件处理”,这种机制主要是为了解决Android应用中多线程的问题,在Android中不允许Activity新启动的线程访问该Activity里的U ...
- Android 中Thread,Handler,Loop学习
1.先看一下最简单的进度条示例 EG: package com.sxz.android.thread; import java.util.concurrent.atomic.AtomicBoolean ...
随机推荐
- [LeetCode] Paint House 粉刷房子
There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...
- [LeetCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- matplotlib 高级用法实例--共享x轴
http://localhost:8888/notebooks/duanqs/matplotlib_advanced_example.ipynb 我不会弄呀, 刚才从matplotlib文档里吧示例用 ...
- JS组件系列——BootstrapTable 行内编辑解决方案:x-editable
前言:之前介绍bootstrapTable组件的时候有提到它的行内编辑功能,只不过为了展示功能,将此一笔带过了,罪过罪过!最近项目里面还是打算将行内编辑用起来,于是再次研究了下x-editable组件 ...
- JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(四):自定义T4模板快速生成页面
前言:上篇介绍了下ko增删改查的封装,确实节省了大量的js代码.博主是一个喜欢偷懒的人,总觉得这些基础的增删改查效果能不能通过一个什么工具直接生成页面效果,啥代码都不用写了,那该多爽.于是研究了下T4 ...
- DataSet与DataTable与DataRow的关系
1. 添加数据: DataRow dr=MyDataSet.Tables["User"].NewRow(); dr["UserName"] = "张三 ...
- call和apply的区别
call和apply都属于function prototype的一个方法. 定义:调用一个方法的对象,以另一个对象替换当前对象. 相同点:两个方法产生的作用是一样的. 不同点:方法传递的参数不同. c ...
- 一次xbuild编译失败的排查
今天一个待上线服务测试完毕,需要构建CI,按照模板配置好包还原,xbuild编译,报错,错误信息如下: EtcdRegister.cs(8,15): error CS0234: The type or ...
- 四.Android adb命令(持续更新...)
1.安装:甭管从哪里下载下来的apk,放在指定的目录下,不一定非要是sdk的目录下:adb install "d:\hxcjaz.apk"(指定的一个目录)2.卸载:adb uni ...
- js修改伪类的值
css文件 p.change:after { content: attr(data-content); } js文件 $(this).addClass('change').attr('data-con ...