转自: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();
//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();

在看下面代码:

  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);
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)比较的更多相关文章

  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. Centos7安装PHP7

    安装依赖 yum updateyum install gcc-c++ libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel lib ...

  2. 当AngularJS POST方法碰上PHP

    问题描述 怎么POST过去给PHP都收不到资料? $_POST方法取不到正确的传入值! 原理说明 AngularJS这套framework使用的AJAX方法中,资料传递的格式为JSON,送出去的hea ...

  3. 创建ejs模板的express工程

    npm install -g express npm install -g express-generator express -e projectName cd projectName npm in ...

  4. oracle日常——数据库备份

    1.进入cmd 2.运行命令 exp [scott]/[orcl]@[orcl] file=[d:\oracle_back\scott_orcl.dmp] owner=scott 格式如下: exp ...

  5. Gone Fishing POJ 1042

    #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> us ...

  6. REDIS持久化报错失败

    redis log报错: [7666] 15 Jan 00:22:36.028 # Error moving temp DB file on the final destination: Invali ...

  7. linux系统下使用流行的版本管理工具 Git

    前几天被版本管理困扰了好久,主要是因为 没法回到之前的版本,新版本又出了问题真的很尴尬. 终于决定使用目前网上很火的版本管理工具-------Git 历史啥的就不说了,说些有用的. 我用的是oschi ...

  8. python网络编程-OSI七层模型详解

    OSI 七层模型通过七个层次化的结构模型使不同的系统不同的网络之间实现可靠的通讯,因此其最主要的功能就是帮助不同类型的主机实现数据传输 . 完成中继功能的节点通常称为中继系统.在OSI七层模型中,处于 ...

  9. EF增删库查

    public async Task<bool> Add(fu_ocrresult model) { using (var db = new GENEModel()) { //1.将实体对象 ...

  10. .net 开源组件

    文章转自:http://www.cnblogs.com/asxinyu/p/dotnet_opensource_project_3.html   在前2篇文章这些.NET开源项目你知道吗?让.NET开 ...