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. Message msg = handler.obtainMessage();
  2. msg.arg1 = i;
  3. msg.sendToTarget();
  1. Message msg=new Message();
  2. msg.arg1=i;
  3. handler.sendMessage(msg);

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

本文转自:http://blog.csdn.net/imdxt1986/article/details/7041570

android之handler obtainmessge与New message区别的更多相关文章

  1. Android笔记——Handler Runnable与Thread的区别

    在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口:Thread类是在java.lang包中定义的.一个类只要继承了Thread类同时覆写了本类中的run() ...

  2. android中handler中 obtainmessge与New message区别

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

  3. android中handler和bundle有什么区别和联系 都是用来传递消息吗都是信息的载体吗

    1.handler是消息处理者,通常重写Handler的handleMessage()方法,在方法中处理接收到的不同消息,例如: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Ha ...

  4. Android开发笔记之:Handler Runnable与Thread的区别详解

    在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口:Thread类是在java.lang包中定义的.一 个类只要继承了Thread类同时覆写了本类中的run( ...

  5. android线程 Handler Message Queue AsyncTask线程模型 线程交互 + 修改Button样式 示例 最终easy整合版

     首先原谅我把文章的标题写的这么长.其实我还嫌弃它短了因为 写不下去了所以我就不写了.因为我实在不知道该怎么定义这篇文章的标题或许应该叫 "乱谈"比较合适. 这样可能还体现了 ...

  6. Android消息机制:Looper,MessageQueue,Message与handler

    Android消息机制好多人都讲过,但是自己去翻源码的时候才能明白. 今天试着讲一下,因为目标是讲清楚整体逻辑,所以不追究细节. Message是消息机制的核心,所以从Message讲起. 1.Mes ...

  7. Android Handler 机制 - Looper,Message,MessageQueue

    Android Studio 2.3 API 25 从源码角度分析Handler机制.有利于使用Handler和分析Handler的相关问题. Handler 简介 一个Handler允许发送和处理M ...

  8. Android——线程通讯 Handler、Looper、Message;

    线程通讯问题 (主要用到了Handler类,Looper类和Message类以及MessageQueue) 在Android中主线程如何向子线程中发送消息的问题.让我们来想想,这其中的过程,无非就是创 ...

  9. W/MessageQueue: Handler (android.os.Handler) {4241f8f8} sending message to a Handler on a dead thread

    缩略信息是: sending message to a Handler on a dead thread 我是用IntentService时报的 稍微纤细一点儿的信息是: Handler (andro ...

随机推荐

  1. evaluateScript--evaluatePopoverScript--区别

    appcan.window.evaluateScript({})              //window.open()页面之间使用 appcan.window.evaluatePopoverScr ...

  2. iOS应用数据存储的常用方式

    iOS应用 数据存储的常用方式 XML属性列表 plist Preference 偏好设置 NSKeyedArchiver 归档 Core Data SQLite3 应用沙盒: Layer:     ...

  3. org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode

    [spring]:org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowe ...

  4. 【Ubuntu12.04】安装搜狗输入法

    我的系统版本是Ubuntu12.04 32位 卸载Ibus输入法 sudo apt-get remove ibus 注意: 安装ibus的命令是  sudo apt-get install fcitx ...

  5. linux安装mysql出现Could NOT find Curses (missing CURSES_LIBRARY CURSES_INCLUDE_PATH),提示解决方法

    [root@localhost mysql-5.5.11]# cmake . 出现以下错误提示: -- Could NOT find Curses (missing:  CURSES_LIBRARY ...

  6. 我的第一个python代码实践:Trie树

    Trie树 不解析,  本园很多博文有提到. 直接上代码: #coding:utf-8 ''' create on 2013-07-30 @author :HuangYanQiang ''' LETT ...

  7. 一步步学习NHibernate(8)——HQL查询(2)

    请注明转载地址:http://www.cnblogs.com/arhat 在上一章中,老魏带着大家学习了HQL语句,发现HQL语句还是非常不错的,尤其是在懒加载的时候,书写起来比较的舒服,但是这里老魏 ...

  8. 图片上传并显示(兼容ie),图片大小判断

    图片上传并显示(兼容ie),图片大小判断 HTML <div id="swf" style="margin: 0 auto;text-align: center;& ...

  9. cocos2dx 3.4 截图代码

    Size size = Director::sharedDirector()->getWinSize(); //定义一个屏幕大小的渲染纹理 RenderTexture* pScreen = Re ...

  10. AVQueuePlayer,备用

    想要视频一个接一个的无缝连续播放么?还在用二逼的mpmovieplayercontroller么?太out了!本大仙介绍一个可以实现无缝连续播放视频的东西-------AVQueuePlayer ! ...