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。

具体代码例如以下:

  1. package djx.android;
  2.  
  3. import djx.downLoad.DownFiles;
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.os.Handler;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10. import android.widget.TextView;
  11.  
  12. public class downLoadPractice extends Activity {
  13. private Button button_submit=null;
  14. private TextView textView=null;
  15. private String content=null;
  16. private Handler handler=null;
  17. /** Called when the activity is first created. */
  18. @Override
  19. public void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22. //创建属于主线程的handler
  23. handler=new Handler();
  24.  
  25. button_submit=(Button)findViewById(R.id.button_submit);
  26. textView=(TextView)findViewById(R.id.textView);
  27. button_submit.setOnClickListener(new submitOnClieckListener());
  28. }
  29. //为按钮加入监听器
  30. class submitOnClieckListener implements OnClickListener{
  31. @Override
  32. public void onClick(View v) {
  33. //本地机器部署为server。从本地下载a.txt文件内容在textView上显示
  34. final DownFiles df=new DownFiles("http://192.168.75.1:8080/downLoadServer/a.txt");
  35. textView.setText("正在载入......");
  36. new Thread(){
  37. public void run(){
  38. content=df.downLoadFiles();
  39. handler.post(runnableUi);
  40. }
  41. }.start();
  42. }
  43.  
  44. }
  45.  
  46. // 构建Runnable对象。在runnable中更新界面
  47. Runnable runnableUi=new Runnable(){
  48. @Override
  49. public void run() {
  50. //更新界面
  51. textView.setText("the Content is:"+content);
  52. }
  53.  
  54. };
  55.  
  56. }

參考网址:

1.
http://blog.csdn.net/djx123456/article/details/6325983

2. 具体线程具体解释链接http://lavasoft.blog.51cto.com/62575/27069/

使用Handler在子线程中更新UI的更多相关文章

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

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

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

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

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

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

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

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

  5. 如何在子线程中更新UI

    一:报错情况 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that creat ...

  6. Android开发UI之在子线程中更新UI

    转自第一行代码-Android Android是不允许在子线程中进行UI操作的.在子线程中去执行耗时操作,然后根据任务的执行结果来更新相应的UI控件,需要用到Android提供的异步消息处理机制. 代 ...

  7. C#子线程中更新ui

    本文实例总结了C#子线程更新UI控件的方法,对于桌面应用程序设计的UI界面控制来说非常有实用价值.分享给大家供大家参考之用.具体分析如下: 一般在winform C/S程序中经常会在子线程中更新控件的 ...

  8. Android 在子线程中更新UI

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

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

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

随机推荐

  1. npm run dev 出现警告

    WARNING in ./node_modules/_webpack@3.10.0@webpack/buildin/global.js There are multiple modules with ...

  2. Python关于super()函数的理解

    看下面的例子: class A: def __init__(self, name): self.name = name def bb(self): print('没事就爱瞎BB') class B(A ...

  3. A - Dubstep

    Problem description Vasya works as a DJ in the best Berland nightclub, and he often uses dubstep mus ...

  4. History of the browser user-agent string--转

    https://webaim.org/blog/user-agent-string-history/ In the beginning there was NCSA Mosaic, and Mosai ...

  5. 第6章 服务模式 在 .NET 中实现 Service Interface

    上下文 您 的应用程序部署在 Microsoft Windows? 操作系统上.您决定将应用程序的某一块功能作为 ASP.NET Web Service 公开.互操作性是一个关键问题,因此您无法使用仅 ...

  6. Oracle表的种类及定义

    1表的类型 1)堆组织表(heap organized tables). 当增加数据时,将使用在段中找到的第一个适合数据大小的空闲空间.当数据从表中删除时,留下的空间允许随后的insert和updat ...

  7. how does Array.prototype.slice.call() work?

    763 down vote accepted +50 What happens under the hood is that when .slice() is called normally, thi ...

  8. ESLint 规范项目代码

    ESLint 由 JavaScript 红宝书 作者 Nicholas C. Zakas 编写, 2013 年发布第一个版本. NCZ 以可扩展.每条规则独立.不内置编码风格为理念编写了一个 lint ...

  9. highcharts例子

    直接看代码 <script language="JavaScript"> $(document).ready(function() { $.ajax({ type:'p ...

  10. 动态给某一个元素添加active

    <li class="one_data" data-id='+ navGroup.self_first_nav[i].id +'><a  href='+ navG ...