使用Handler在子线程中更新UI
Android规定仅仅能在主线程中更新UI。假设在子线程中更新UI 的话会提演示样例如以下错误:Only the original thread that created a view hierachy can touch its view((仅仅有原来的线程创建一个视图层次能够触摸它的视图)。
仅仅能在主线程中更新UI的原因是:android中相关的view和控件不是线程安全的,我们必须单独做处理。
有的时候须要再子线程中实现更新UI,以下介绍使用Handler实现线程通信的特点实如今子线程中更新UI。
Handler的使用场合:
1、 to schedule messages and runnables to be executed as some point in the future;
安排messages和runnables在将来的某个时间点运行。
2、 to enqueue an action to be performed on a different thread than your own.
将action入队以备在一个不同的线程中运行。即能够实现线程间通信。比方当你创建子线程时。你能够再你的子线程中拿到父线程中创建的Handler对象,就能够通过该对象向父线程的消息队列发送消息了。
因为Android要求在UI线程中更新界面,因此,能够通过该方法在其他线程中更新界面。
子线程更新UI实例:
步骤:
1、创建Handler对象(此处创建于主线程中便于更新UI)。
2、构建Runnable对象。在Runnable中更新界面。
3、在子线程的run方法中向UI线程post,runnable对象来更新UI。
具体代码例如以下:
package djx.android; import djx.downLoad.DownFiles;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView; public class downLoadPractice extends Activity {
private Button button_submit=null;
private TextView textView=null;
private String content=null;
private Handler handler=null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建属于主线程的handler
handler=new Handler(); button_submit=(Button)findViewById(R.id.button_submit);
textView=(TextView)findViewById(R.id.textView);
button_submit.setOnClickListener(new submitOnClieckListener());
}
//为按钮加入监听器
class submitOnClieckListener implements OnClickListener{
@Override
public void onClick(View v) {
//本地机器部署为server。从本地下载a.txt文件内容在textView上显示
final DownFiles df=new DownFiles("http://192.168.75.1:8080/downLoadServer/a.txt");
textView.setText("正在载入......");
new Thread(){
public void run(){
content=df.downLoadFiles();
handler.post(runnableUi);
}
}.start();
} } // 构建Runnable对象。在runnable中更新界面
Runnable runnableUi=new Runnable(){
@Override
public void run() {
//更新界面
textView.setText("the Content is:"+content);
} }; }
參考网址:
1.
http://blog.csdn.net/djx123456/article/details/6325983
2. 具体线程具体解释链接http://lavasoft.blog.51cto.com/62575/27069/
使用Handler在子线程中更新UI的更多相关文章
- Android在子线程中更新UI(二)
MainActivity如下: package cc.testui2; import android.os.Bundle; import android.view.View; import andro ...
- Android在子线程中更新UI(一)
MainActivity如下: package cc.testui1; import android.os.Bundle; import android.os.Handler; import andr ...
- android 不能在子线程中更新ui的讨论和分析
问题描写叙述 做过android开发基本都遇见过 ViewRootImpl$CalledFromWrongThreadException,上网一查,得到结果基本都是仅仅能在主线程中更改 ui.子线程要 ...
- Android多线程之(一)View.post()源码分析——在子线程中更新UI
提起View.post(),相信不少童鞋一点都不陌生,它用得最多的有两个功能,使用简便而且实用: 1)在子线程中更新UI.从子线程中切换到主线程更新UI,不需要额外new一个Handler实例来实现. ...
- 如何在子线程中更新UI
一:报错情况 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that creat ...
- Android开发UI之在子线程中更新UI
转自第一行代码-Android Android是不允许在子线程中进行UI操作的.在子线程中去执行耗时操作,然后根据任务的执行结果来更新相应的UI控件,需要用到Android提供的异步消息处理机制. 代 ...
- C#子线程中更新ui
本文实例总结了C#子线程更新UI控件的方法,对于桌面应用程序设计的UI界面控制来说非常有实用价值.分享给大家供大家参考之用.具体分析如下: 一般在winform C/S程序中经常会在子线程中更新控件的 ...
- Android 在子线程中更新UI
今天在做练习时,在一个新开启的线程中调用“Toast.makeText(MainActivity.this, "登陆成功",Toast.LENGTH_SHORT).show();” ...
- 老问题:Android子线程中更新UI的3种方法
在Android项目中经常有碰到这样的问题,在子线程中完成耗时操作之后要更新UI,下面就自己经历的一些项目总结一下更新的方法: 方法一:用Handler 1.主线程中定义Handler: Handle ...
随机推荐
- TestNG环境搭建以及框架初识
TestNG的英文为Test Next Generation, 听上去好像下一代测试框架已经无法正常命名了的样子,哈哈,言归正传,啥是TestNG呢,它是一套测试框架,在原来的Junit框架的思想基础 ...
- 2015 多校赛 第一场 1001 (hdu 5288)
Description OO has got a array A of size n ,defined a function f(l,r) represent the number of i (l&l ...
- 控件——DataGridview
控件:DataGridview 用来显示数据, 可以显示和编辑来自多种不同类型的数据源的表格数据. 一.两种显示数据的方式:手动,后台代码 主要通过后台代码:先建立三大类 然后绑定 ...
- 抽象工厂模式(AbsFactory)C++实现
模式意图:提供一个创建一系列相关或相互依赖对象的接口,二无需指定他们具体的类. 效果: 分离了具体的类. 使 a.客户与类的实现分离 b.客户通过抽象接口操纵实例 c.产品的类名在实现中 ...
- Android一对多蓝牙连接示例APP
一对多蓝牙连接示例,基于Google BluetoothChat修改,实现一对多聊天(一个服务端.多个客户端),类似聊天室. 主要功能: 客户端的发出的消息所有终端都能收到(由服务端转发) 客户端之间 ...
- CUDA与OpenCL架构
CUDA与OpenCL架构 目录 CUDA与OpenCL架构 目录 1 GPU的体系结构 1.1 GPU简介 1.2 GPU与CPU的差异 2 CUDA架构 2.1 硬件架构 2.1.1 GPU困境 ...
- [ Linux ] [ OS ] [ memory ] Linux 如何查看系統硬體的記憶體(RAM)資訊
cat /proc/meminfo https://blog.longwin.com.tw/2013/05/linux-ram-memory-info-2013/
- 图像压缩Vs.压缩感知
压缩感知科普文两则: 原文链接:http://www.cvchina.info/2010/06/08/compressed-sensing-2/ 这几天由于happyharry的辛勤劳动,大伙纷纷表示 ...
- spring注解略解
@Scope("prototype") 如果ACTION中不@Scope("prototype"),有可能报找不到XXXACTION的错误!写上这个就表示每次请 ...
- BZOJ 1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏 幼儿园测试题
本来以为是一道数学题,一顿XJBT导式子,结果就是个幼儿园都会的模拟. Code: #include<bits/stdc++.h> #define ll long long using n ...