如果你不带参数的实例化:Handler handler=new Handler();那么这个会默认用当前线程的Looper对象。

一般而言,如果你的Handler是要用来刷新UI的,那么就需要在主线程下运行。

情况:

1 要 刷新UI,handler要用到主线程的Looper对象。那么在主线程Handler handler=new Handler()  如果在其他非主线程也要满足这个功能的话,要Handler  handler=new Handler(Looper.getMainLooper());

2 不用刷新UI ,只是处理消息。当前消息如果是主线程的话,Handler handler=new Handler ;不知主线程的话,Looper.prepare() Handler handler=new Handler();Looper.loop()   或者Handler  handler=new Handle(Looper.getMainLooper());

若是实例化的时候调用Looper.getMainLooper()就表示放到主线程中去处理。

若有不是的话,因为只有UI 线程默认Loop.prepare()  Loop.loop()过,其他线程需要手动调用这两个。否则会报错。

关于new Handler()与new Handler(Looper.getMainLooper())区别的更多相关文章

  1. Handler一定要在主线程实例化吗?new Handler()和new Handler(Looper.getMainLooper())的区别?

    一个帖子的整理: Handler一定要在主线程实例化吗?new Handler()和new Handler(Looper.getMainLooper())的区别如果你不带参数的实例化:Handler ...

  2. new Handler()和new Handler(Looper.getMainLooper())的区别

    一个帖子的整理: Handler一定要在主线程实例化吗?new Handler()和new Handler(Looper.getMainLooper())的区别如果你不带参数的实例化:Handler ...

  3. new Handler()和new Handler(Looper.getMainLooper())的区别是什么?

    new Handler()和new Handler(Looper.getMainLooper())的区别是什么?     一.Handler的一些知识,new Handler()和new Handle ...

  4. Handler,Thread,Looper之间关系小结

    http://blog.csdn.net/sunxingzhesunjinbiao/article/details/6794840 (1) Looper类别用来为一个线程开启一个消息循环.默认情况下A ...

  5. android 消息系统Handler、MessageQueue、Looper源代码学习

    android消息系统 总体框架如图所看到的 在安卓的消息系统中,每一个线程有一个Looper,Looper中有一个MessageQueue,Handler向这个队列中投递Message,Looper ...

  6. Android的消息处理机制,handler,message,looper(一)

    当应用程序启动时,Android首先会开启一个主线程(也就是UI线程),主线程为管理界面中的UI控件.在程序开发时,对于比较耗时的操作,通常会为其开辟一个单独的线程来执行,以尽可能减少用户的等待时间. ...

  7. Handler和Message以及Looper之间的三角关系

    说到Handler想必大家都经常用到,在非UI线程更新UI那可是利器,用起来也非常容易上手 从使用上来说,我们只需要关注sendMessage和handleMessage即可 所以我们先从Handle ...

  8. Android线程之异步消息处理机制(二)——Message、Handler、MessageQueue和Looper

    异步消息处理机制解析 Android中的异步消息处理主要有四个部分组成,Message.Handler.MessageQueue和Looper. 1.Message Message是在线程之间传递的消 ...

  9. Android 消息机制 (Handler、Message、Looper)

    综合:http://blog.csdn.net/dadoneo/article/details/7667726 与 http://android.tgbus.com/Android/androidne ...

随机推荐

  1. 一条慢SQL引发的血案

    直接切入正题吧: 通常来说,我们看到的慢查询一般还不致于导致挂站,顶多就是应用响应变慢不过这个恰好今天被我撞见了,一个慢查询把整个网站搞挂了先看看这个SQL张撒样子: # Query_time: 70 ...

  2. There is no getter for property named 'notice' in 'class com.game.domain.Notices'

    在插入数据时报错:There is no getter for property named 'notice' in 'class com.game.domain.Notices' 四月 11, 20 ...

  3. java的4种引用 强软弱虚

    <img src="https://pic4.zhimg.com/d643d9ab5c933ac475cfa23063bed137_b.png" data-r ...

  4. Java8 新特性Stream 的学习和使用方法

    流(Stream) 流是java 8 中新引入的特性,用来处理集合中的数据,Stream 是一个来自数据源的元素队列并支持聚合操作. Java 中 Stream 不会存储元素. 数据源 流的来源. 可 ...

  5. python之冒泡排序

    重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成.def Bubble_Sort(lists): ...

  6. Android 捕获组合键

    android中捕获组合键http://blog.csdn.net/wenlibin1985/article/details/5579359 Android组合键http://www.eoeandro ...

  7. Using Immutable in React + React-Redux

    React-Redux Introduction React-Redux is a library for React based on Redux package. And the core ide ...

  8. JsonTools

    public class JsonTools { /// <summary> /// Generate Json string from the object /// </summa ...

  9. (转)编码规范系列(一):Eclipse Code Templates设置

    背景:长久以来,对java编程中的注释不甚理解.再次学习<疯狂JAVA讲义>基础,深深的感到自己基本功的不牢固.所以要做到事无巨细,好好修炼. 认识注释 常识 注释的作用: 回顾原有的代码 ...

  10. python入门:求1-2+3-4+5...99的所有数的和

    #!/usr/bin/env python # -*- coding:utf-8 -*- #求1-2+3-4+5...99的所有数的和 """ 给start赋值为1,su ...