利用Activity.runOnUiThread(Runnable)把更新ui的代码创建在Runnable中,然后在需要更新ui时,把这个Runnable对象传给Activity.runOnUiThread(Runnable).

Runnable对像就能在ui程序中被调用。

/*** Runs the specified action on the UI thread. If the current thread is the UI* thread, then the action is executed immediately. If the current thread is* not the UI thread, the action is posted to the event queue of the UI thread.** @param action the action to run on the UI thread*/public final void runOnUiThread(Runnable action) {if (Thread.currentThread() != mUiThread) {mHandler.post(action);} else {action.run();}}

从上面的源代码中可以看出,程序首先会判断当前线程是否是UI线程,如果是就直接运行,如果不是则post,这时其实质还是使用的Handler机制来处理线程与UI通讯。

        private ProgressDialog progressDialog;        Context mContext;

        progressDialog = new ProgressDialog(mContext);String stri = mContext.getResources().getString(R.string.Is_sending_a_request);progressDialog.setMessage(stri);progressDialog.setCanceledOnTouchOutside(false);progressDialog.show();new Thread(new Runnable() {public void run() {try {((Activity)mContext).runOnUiThread(new Runnable() {public void run() {progressDialog.dismiss();String s1 = mContext.getResources().getString(R.string.send_successful);Toast.makeText(mContext, s1, Toast.LENGTH_LONG).show();}});} catch (final Exception e) {((Activity)mContext).runOnUiThread(new Runnable() {public void run() {progressDialog.dismiss();String s2 = mContext.getResources().getString(R.string.Request_add_buddy_failure);Toast.makeText(mContext, s2 + e.getMessage(), Toast.LENGTH_LONG).show();}});}}}).start();

用这种方式创建ProgressDialog就比较方便,或者刷新adapter也比使用Thread+Handler方便。

如果不是在activity中创建,需要在前面加上((Activity)mContext). 。

转载于:https://www.cnblogs.com/tangZH/p/6107556.html

android Activity runOnUiThread() 方法的使用相关推荐

  1. android Activity runOnUiThread() 方法使用

    在android 中我们一般用 Handler 做主线程 和 子线程 之间的通信 . 现在有了一种更为简洁的写法,就是 Activity 里面的 runOnUiThread( Runnable )方法 ...

  2. Android Activity onCreate()方法重复执行

    摘要: Android程序的Activity的onCreate()重复执行,导致界面多次加载,影响用户体验. 问题描述: 1.程序运行在平板上(有底座,如图1-1 底座有串口.USB口.电源口.以太网 ...

  3. Android Activity为什么要细化出onCreate、onStart、onResume、onPause、onStop、onDesdroy这么多方法让应用去重载?

    Android Activity为什么要细化出onCreate.onStart.onResume.onPause.onStop.onDesdroy这么多方法让应用去重载? 原创链接:http://bl ...

  4. android activity查询,android中activity.findViewById()方法查找的是什么?

    //代码1: package com.example.testsdk2; import android.app.Activity; import android.content.Intent; imp ...

  5. android 动画方式,Android Activity进出动画三种方法

    Android Activity进出动画三种方法 实现activity的进出场动画总共有3种方式,下面会一一列出,首先给出示例的动画xml文件. 动画的xml文件 android:fromYDelta ...

  6. android获取activity截图,Android Activity 不能被截屏的解决方法

    Android Activity 不能被截屏的解决方法 在Activity 添加即可 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECU ...

  7. Android基础之结束所有Activity通用方法

    出处:http://blog.csdn.net/chenliguan/article/details/47295163 一.问题描述 做Android项目,退出程序时,有时不能退出所有的activit ...

  8. 理解 Activity.runOnUiThread

    在开发 Android 应用的时候我们总是要记住应用主线程. 主线程非常繁忙,因为它要处理绘制UI,响应用户的交互,默认情况下执行我们写下的大部分代码. 好的开发者知道他/她需要将重负荷的任务移除到工 ...

  9. 【转】Android Activity原理以及其子类描述,androidactivity

        Android Activity原理以及其子类描述,androidactivity 简介 Activity是Android应用程序组件,实现一个用户交互窗口,我们可以实现布局填充屏幕,也可以实 ...

最新文章

  1. Android移动开发之【Android实战项目】渐变的TextView小漂亮代码
  2. mysql dump 查看器_mysql备份之mysqldump工具
  3. Python爬虫-Scrapy-CrawlSpider与ItemLoader
  4. 怎么实现登录之后跳转到登录之前的页面?SpringMVC+Freemarker
  5. mysql 内存溢出_mysql - MySQL在非常大的表上计算性能 - 堆栈内存溢出
  6. linux-centos7 关机命令、系统目录结构介绍
  7. 如下为利用Linux内核链表创建,Linux内核中链表的实现与应用
  8. JavaScript 获取当日在今年第几周
  9. 深入理解mysql中case when流程控制语句
  10. 天天打无人车是怎样一种体验?
  11. TLStorm:APC UPS 存在零点击0day,可远程烧毁设备、切断电源
  12. 26个最好免费下载电子书(Ebooks)的网站
  13. 词频统计 matlab,5年cvpr论文词频分析,今年最火词——深度学习
  14. PowerApps社区计划
  15. (附源码)计算机毕业设计SSM原创网络文学管理系统
  16. 简单实用算法——二分查找法(BinarySearch)
  17. 1010. Pairs of Songs With Total Durations Divisible by 60*
  18. 【考研经验】2018四跨吉林大学计算机初试复试经验贴(67+72+99+141=379分)
  19. 系统动力学专拓考试重点总结
  20. 2018华为软件精英挑战赛经验(一)

热门文章

  1. tm是什么域名_入驻天猫,需要什么条件?
  2. iso22000食品安全管理体系_食品安全管理体系ISO22000:2018澄清了两个层次PDCA的区别,具体怎么样的...
  3. 四川南充计算机经贸学校,四川南充工贸学校计算机应用专业秋季计划
  4. superset ubuntu16.04 python3 安装
  5. 华为的JAVA面试题及答案(部分)
  6. Java案例:基于TCP的简单聊天程序
  7. 【BZOJ3238】差异,后缀数组+单调栈维护height
  8. 【多题合集】AC自动机练习,被HDU支配的恐惧
  9. 【codevs1166】【noip07TG】矩阵取数游戏,新的开始
  10. 三角学——极坐标_2