转载请标明出处:https:////www.cnblogs.com/tangZH/p/6107556.html

更多精彩文章: http://77blogs.com/?p=138

利用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). 。

android Activity runOnUiThread() 方法的使用的更多相关文章

  1. android Activity runOnUiThread() 方法使用

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

  2. Handler具体解释系列(七)——Activity.runOnUiThread()方法具体解释

    MainActivity例如以下: package cc.testui3; import android.os.Bundle; import android.view.View; import and ...

  3. Android Activity的加载模式和onActivityResult方法之间的冲突

    前言 今天在调试程序时,发现在某一Activity上点击返回键会调用该Activity的onActivityResult()方法.我一开始用log,后来用断点跟踪调试半天,还是百思不得其解.因为之前其 ...

  4. Android Activity的onSaveInstanceState() 和 onRestoreInstanceState()方法:

    Android Activity的onSaveInstanceState() 和 onRestoreInstanceState()方法: 1. 基本作用: Activity的 onSaveInstan ...

  5. 从零开始学android开发-用Intent启动Activity的方法

    启动另外一个Activity,可以有的方法有用setClass()和Component Name 1. 先说在setClass启动一个Activity的方法吧: Intent intent = new ...

  6. [转]Android Activity的加载模式和onActivityResult方法之间的冲突

    前言 今天在调试程序时,发现在某一Activity上点击返回键会调用该Activity的onActivityResult()方法.我一开始用log,后来用断点跟踪调试半天,还是百思不得其解.因为之前其 ...

  7. 我的Android进阶之旅------>Android Activity的singleTask加载模式和onActivityResult方法之间的冲突

    今天调试一个bug的时候,情景如下: 一个Activity A,需要用startActivityForResult方法开启Activity B.Activity B的launch mode被设置为si ...

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

    原文:http://www.xuebuyuan.com/1608083.html 最近在研究Activity的启动流程,老罗的blog在看,也找了其它资料学习,也跟过Android4.3的源码, 在跟 ...

  9. activity.runOnUiThread()内的run()方法没有被执行

    activity.runOnUiThread(new Runnable() { public void run() { Toast.makeText(context, toast, Toast.LEN ...

随机推荐

  1. Ubuntu下利用Mono,Jexus搭建Asp.Net(MVC) Web服务器

    最近在Ubuntu上搭建了Asp.Net的Web服务器,其中遇到很多问题,整理一下思路,以备后用. 搭建环境以及配套软件 Ubuntu: 11.10 Mono:3.0.6 下载地址(http://do ...

  2. Linux 中优秀的文本化编辑思想大碰撞(Markdown、LaTeX、MathJax)

    这样一个标题可能不太准确,因为确实无法准确地解释什么叫"文本化编辑思想".其实我这篇随笔主要是想探讨 Markdown.LaTeX.MathJax,有兴趣的朋友可以继续往下看,同时 ...

  3. CSharpGL(36)通用的非托管数组排序方法

    CSharpGL(36)通用的非托管数组排序方法 如果OpenGL要渲染半透明物体,一个方法是根据顶点到窗口的距离排序,按照从远到近的顺序依次渲染.所以本篇介绍对 UnmanagedArray< ...

  4. ceph架构剖析

    unitedstack有云 :https://www.ustack.com/blog/ceph_infra/

  5. iOS----应用的旋转---Orientations

    此博文主要针对IOS应用, 是屏幕旋转相关问题的一个总结. 主要内容有: IOS5,6,7不同版的适配. 强制旋转和自动旋转. QQ : 1101819159 邮箱: GeekiVan@aliyun. ...

  6. Java发展历史

    1991年1月 Sun公司成立了Green项目小组,专攻智能家电的嵌入式控制系统 1991年2月 放弃C++,开发新语言,命名为"Oak" 1991年6月 JamesGosling ...

  7. ASP.NET OAuth:access token的加密解密,client secret与refresh token的生成

    在 ASP.NET OWIN OAuth(Microsoft.Owin.Security.OAuth)中,access token 的默认加密方法是: 1) System.Security.Crypt ...

  8. JavaScript 解决 onblur 与 onclick 冲突

    <input type="text" onblur="function1" /> <input type="button" ...

  9. Solr Facet 默认值

    前言 今天在用Solr Facet遇到了默认值的问题,我用Facet.field查询发现数据总共100条,刚开始没有注意,发现少个别数据,但是用这几个个别的id查询又能查出来数据.才发现是Facet默 ...

  10. linux内核数据结构之kfifo

    1.前言 最近项目中用到一个环形缓冲区(ring buffer),代码是由linux内核的kfifo改过来的.缓冲区在文件系统中经常用到,通过缓冲区缓解cpu读写内存和读写磁盘的速度.例如一个进程A产 ...