转自:http://iaiai.iteye.com/blog/1992196

obtainmessage()是从消息池中拿来一个msg 不需要另开辟空间new
new需要重新申请,效率低,obtianmessage可以循环利用;

  1. //use Handler.obtainMessage(),instead of msg = new Message();
  2. //because if there is already an Message object,that not be used by
  3. //any one ,the system will hand use that object,so you don't have to
  4. //create and object and allocate memory.
  5. //it  is also another example of object recycling and reusing in android.
  6. Message msg = mHandler.obtainMessage();
  7. msg.what = UPDATE_LISTVIEW;
  8. msg.obj = current + "/" + total + "songs";
  9. //this method is called from worker Thread,so we cannot update UI from here.
  10. msg.sendToTarget();
  1. //use Handler.obtainMessage(),instead of msg = new Message();
  2. //because if there is already an Message object,that not be used by
  3. //any one ,the system will hand use that object,so you don't have to
  4. //create and object and allocate memory.
  5. //it is also another example of object recycling and reusing in android.
  6. Message msg = mHandler.obtainMessage();
  7. msg.what = UPDATE_LISTVIEW;
  8. msg.obj = current + "/" + total + "songs";
  9. //this method is called from worker Thread,so we cannot update UI from here.
  10. msg.sendToTarget();

在看下面代码:

  1. Message msg = handler.obtainMessage();
  2. msg.arg1 = i;
  3. msg.sendToTarget();
  4. Message msg=new Message();
  5. msg.arg1=i;
  6. handler.sendMessage(msg);
  1. Message msg = handler.obtainMessage();
  2. msg.arg1 = i;
  3. msg.sendToTarget();
  4.  
  5. Message msg=new Message();
  6. msg.arg1=i;
  7. handler.sendMessage(msg);

第一种写法是message 从handler 类获取,从而可以直接向该handler 对象发送消息,第二种写法是直接调用 handler 的发送消息方法发送消息。

Handler sendMessage 与 obtainMessage (sendToTarget)比较的更多相关文章

  1. Handler sendMessage 与 obtainMessage (sendToTarget)

    这篇文章讲的很好: http://www.cnblogs.com/android007/archive/2012/05/10/2494766.html 两种用法: 1. private void se ...

  2. Android sendMessage 与 obtainMessage (sendToTarget)比较

    话说在工作中第一次接触android 的Handler 的时候,不知道怎么去关注性能. 记得当时这么写的: Message msg = new Message() msg.what = xxx; ms ...

  3. sendMessage 与 obtainMessage (sendToTarget)比较

    我们平时在做到多线程问题的时候可能利用Handler去传递Message,其中,经常使用的就是 1.new Handler().obtainMessage().sendToTarget(); 2.ne ...

  4. Handler.sendMessage 与 Handler.obtainMessage.sendToTarget比较

    原文地址: http://www.cnblogs.com/android007/archive/2012/05/10/2494766.html 话说在工作中第一次接触android 的Handler ...

  5. 91、sendToTarget与sendMessage

    Message msg = handler.obtainMessage();               msg.arg1 = i;               msg.sendToTarget(); ...

  6. Android handler.obtainMessage()

    在handler.obtainMessage()的参数是这样写的: Message android.os.Handler.obtainMessage(int what, int arg1, int a ...

  7. 【转】Handler学习笔记(二)

    一.一个问题 有这样一个问题值得我们思考,若把一些类似于下载的功能(既耗时且不一定有结果)写在Activity(主线程)里,会导致Activity阻塞,长时间无响应,直至页面假死(如果5秒钟还没有完成 ...

  8. Android--多线程之Handler

    前言 Android的消息传递机制是另外一种形式的“事件处理”,这种机制主要是为了解决Android应用中多线程的问题,在Android中不允许Activity新启动的线程访问该Activity里的U ...

  9. Android 中Thread,Handler,Loop学习

    1.先看一下最简单的进度条示例 EG: package com.sxz.android.thread; import java.util.concurrent.atomic.AtomicBoolean ...

随机推荐

  1. Django基础之安装配置

    安装配置 一 MVC和MTV模式 著名的MVC模式:所谓MVC就是把web应用分为模型(M),控制器(C),视图(V)三层:他们之间以一种插件似的,松耦合的方式连接在一起. 模型负责业务对象与数据库的 ...

  2. 利用sharding-jdbc分库分表

    sharding-jdbc是当当开源的一款分库分表的数据访问层框架,能对mysql很方便的分库.分表,基本不用修改原有代码,只要配置一下即可,完整的配置参考以下内容: <?xml version ...

  3. Binding笔记

    Binding基础  绑定某个对象的属性值到控制上,写法如下: public class Order : INotifyPropertyChanged//只要实现此接口 { public event ...

  4. rank()函数的使用

    排序: ---rank()over(order by 列名 排序)的结果是不连续的,如果有4个人,其中有3个是并列第1名,那么最后的排序结果结果如:1 1 1 4select scoreid, stu ...

  5. jQuery 树形菜单

    树形菜单 在 jQuery easyu中其左侧的主菜单使用的是 easyui 中的 tree 组件,不是太熟悉,不过感觉不是太好用. 比如 easyui 中的 tree 需要单击分叉节点前的小三角,才 ...

  6. Your app declares support for audio in the UIBackgroundModes key in your Info.plist 错误

    提交AppStore时候被拒绝 拒绝原因:Your app declares support for audio in the UIBackgroundModes key in your Info.p ...

  7. js判断浏览器类型

    使用navigator.userAgent和来判断 PC端: <script type="text/javascript">var ua=navigator.userA ...

  8. C#计算一段程序运行时间的三种方法

    第一种方法利用System.DateTime.Now: static void SubTest() { DateTime beforDT = System.DateTime.Now; //耗时巨大的代 ...

  9. mybatis 一对一与一对多collection和association的使用

    在mybatis如何进行一对一.一对多的多表查询呢?这里用一个简单的例子说明. 一.一对一 1.association association通常用来映射一对一的关系,例如,有个类user,对应的实体 ...

  10. pecl 轻松安装php扩展

    PECL 的全称是 The PHP Extension Community Library ,是一个开放的并通过 PEAR(PHP Extension and Application Reposito ...