①使用Activity中的runOnUiThread(Runnable)

②使用Handler中的post(Runnable)

在创建Handler对象时,必须先通过Context的getMainLooper()获取到主循环器(main looper)

例:new Handler(getMainLooper()).post(action)

③使用Handler中的sendMessage(Message)

例:

1.先创建Handler对象

    private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//更新UI的操作
String text = (String) msg.obj;
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
}
};

2.通过Handler发送Message

  Message msg = handler.obtainMessage();
  msg.obj = "上下文包装器\nContext Wrapper";
  handler.sendMessage(msg);

其中,handler.obtainMessage()比直接创建Message(new Message())更有效率

也可替换为Message.obtain()

总结:

①最简便,但是依赖于Activity

②稍复杂,与①类似,依赖于Context

③最复杂,但是不依赖于上下文

Android:在子线程中更新UI的三种方式的更多相关文章

  1. Android 在子线程中更新UI的几种方法

    第一种: new Handler(context.getMainLooper()).post(new Runnable() { @Override public void run() { // 在这里 ...

  2. Android在子线程中更新UI(二)

    MainActivity如下: package cc.testui2; import android.os.Bundle; import android.view.View; import andro ...

  3. Android在子线程中更新UI(一)

    MainActivity如下: package cc.testui1; import android.os.Bundle; import android.os.Handler; import andr ...

  4. 老问题:Android子线程中更新UI的3种方法

    在Android项目中经常有碰到这样的问题,在子线程中完成耗时操作之后要更新UI,下面就自己经历的一些项目总结一下更新的方法: 方法一:用Handler 1.主线程中定义Handler: Handle ...

  5. Android子线程中更新UI的4种方法

    方法一:用Handler 1.主线程中定义Handler: Handler mHandler = new Handler() { @Override public void handleMessage ...

  6. Android 在子线程中更新UI

    今天在做练习时,在一个新开启的线程中调用“Toast.makeText(MainActivity.this, "登陆成功",Toast.LENGTH_SHORT).show();” ...

  7. android 不能在子线程中更新ui的讨论和分析

    问题描写叙述 做过android开发基本都遇见过 ViewRootImpl$CalledFromWrongThreadException,上网一查,得到结果基本都是仅仅能在主线程中更改 ui.子线程要 ...

  8. Android多线程之(一)View.post()源码分析——在子线程中更新UI

    提起View.post(),相信不少童鞋一点都不陌生,它用得最多的有两个功能,使用简便而且实用: 1)在子线程中更新UI.从子线程中切换到主线程更新UI,不需要额外new一个Handler实例来实现. ...

  9. 使用Handler在子线程中更新UI

    Android规定仅仅能在主线程中更新UI.假设在子线程中更新UI 的话会提演示样例如以下错误:Only the original thread that created a view hierach ...

随机推荐

  1. 有关git的配置

    git版本控制器总结 关于部分内容请参考:https://www.cnblogs.com/smuxiaolei/p/7484678.htmlgit是一个版本控制器,分布式管理:可以记录每次文件的改动, ...

  2. Dialog共通写法(两个button)

    package jp.co.hyakujushibank.view import android.app.Dialogimport android.content.Contextimport andr ...

  3. 将RabbitMq用好需要了解的一些基础知识

    本文面向有一定RabbitMq基础的童鞋. 首先,我们来理理RabbitMq的一些基本概念: Connection: 客户端与RabbitMq服务器节点的Tcp链接. Channel: 信道,因为一条 ...

  4. 【Codeforces Round #505 (Div. 1 + Div. 2) 】

    A:https://www.cnblogs.com/myx12345/p/9843966.html B: C:https://www.cnblogs.com/myx12345/p/9844084.ht ...

  5. Valentine's Day Round hdu 5176 The Experience of Love [好题 带权并查集 unsigned long long]

    传送门 The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  6. iOS 设置RGB色的宏

    转自:http://lizhuang.iteye.com/blog/1931768 
//RGB Color macro #define UIColorFromRGB(rgbValue) [UICol ...

  7. 533. Lonely Pixel II

    Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...

  8. codevs——2370 小机房的树

    2370 小机房的树  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 小机房有棵焕狗种的树,树上有N个 ...

  9. python多线程(一)

    原文:http://www.pythonclub.org/python-basic/threading 一.python多线程thread和threading实现 python是支持多线程的,并且是n ...

  10. (入门SpringBoot)SpringBoot项目数据源以及整合mybatis(二)

    1.配置tomcat数据源: #   数据源基本配置spring.datasource.url=jdbc:mysql://localhost:3306/shoptest?useUnicode=true ...