android Activity runOnUiThread() 方法使用
在android 中我们一般用 Handler 做主线程 和 子线程 之间的通信 。
现在有了一种更为简洁的写法,就是 Activity 里面的 runOnUiThread( Runnable )方法。
利用Activity.runOnUiThread(Runnable)把更新ui的代码创建在Runnable中,然后在需要更新ui时,把这个Runnable对象传给Activity.runOnUiThread(Runnable)。
package com.app; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //创建一个线程
new Thread(new Runnable() { @Override
public void run() { //延迟两秒
try {
Thread.sleep( 2000 );
} catch (InterruptedException e) {
e.printStackTrace();
} runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "hah", Toast.LENGTH_SHORT).show();
}
}); }
}).start();
}
}
Activity的runOnUiThread(Runnable)
/**
* 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();
}
}
android Activity runOnUiThread() 方法使用的更多相关文章
- android Activity runOnUiThread() 方法的使用
利用Activity.runOnUiThread(Runnable)把更新ui的代码创建在Runnable中,然后在需要更新ui时,把这个Runnable对象传给Activity.runOnUiThr ...
- Handler具体解释系列(七)——Activity.runOnUiThread()方法具体解释
MainActivity例如以下: package cc.testui3; import android.os.Bundle; import android.view.View; import and ...
- Android Activity的加载模式和onActivityResult方法之间的冲突
前言 今天在调试程序时,发现在某一Activity上点击返回键会调用该Activity的onActivityResult()方法.我一开始用log,后来用断点跟踪调试半天,还是百思不得其解.因为之前其 ...
- Android Activity的onSaveInstanceState() 和 onRestoreInstanceState()方法:
Android Activity的onSaveInstanceState() 和 onRestoreInstanceState()方法: 1. 基本作用: Activity的 onSaveInstan ...
- 从零开始学android开发-用Intent启动Activity的方法
启动另外一个Activity,可以有的方法有用setClass()和Component Name 1. 先说在setClass启动一个Activity的方法吧: Intent intent = new ...
- [转]Android Activity的加载模式和onActivityResult方法之间的冲突
前言 今天在调试程序时,发现在某一Activity上点击返回键会调用该Activity的onActivityResult()方法.我一开始用log,后来用断点跟踪调试半天,还是百思不得其解.因为之前其 ...
- 我的Android进阶之旅------>Android Activity的singleTask加载模式和onActivityResult方法之间的冲突
今天调试一个bug的时候,情景如下: 一个Activity A,需要用startActivityForResult方法开启Activity B.Activity B的launch mode被设置为si ...
- Android Activity为什么要细化出onCreate、onStart、onResume、onPause、onStop、onDesdroy这么多方法让应用去重载?
原文:http://www.xuebuyuan.com/1608083.html 最近在研究Activity的启动流程,老罗的blog在看,也找了其它资料学习,也跟过Android4.3的源码, 在跟 ...
- activity.runOnUiThread()内的run()方法没有被执行
activity.runOnUiThread(new Runnable() { public void run() { Toast.makeText(context, toast, Toast.LEN ...
随机推荐
- Android聚合广告AFP的对接系统设计
工作需要,要对接阿里妈妈的广告聚合平台,简称AFP.对于一般的应用而言,想要流量变现,广告是显而易见的手段,尤其是在中国,打开一个千万级别的用户,肯定有某个地方是有对接广告的,只不过明不明显而已. 阿 ...
- SpringAOP使用扩展
在<初识Spring>中我们了解到Spring支持AOP且可配置方法的前置曾强和后置曾强,但其实Spring支持多种曾强类型.下面同过一些例子来介绍Spring的几种常用的曾强(前置增强和 ...
- C# CGI程序
一.控制面板—>程序和功能—>打开或关闭Windows功能 把相关的功能勾上,点“确定” 二.新建一个网站,配置ISAPI和CGI限制.处理程序映射 三.CGI控制台应用程序代码: usi ...
- 激活当前视图菜单高亮呈现 V2.0
前一段时间,Insus.NET有分享一篇<激活当前视图菜单高亮呈现>http://www.cnblogs.com/insus/p/5287093.html 这篇只是同一控制器的菜单. 今天 ...
- ASP.NET入门教程:事件句柄
事件句柄(event handler)是一种针对给定事件来执行代码的子例程. ASP.NET - 事件句柄 请看下面的代码: <% lbl1.Text="The date and ti ...
- ActionSupport与action区别
action是接口,只有一个execute方法需要实现.ActionSupport是action接口的一个实现类.这个类除了实现action接口还实现了Validateable(用于验证)等接口,开发 ...
- When to close cursors using MySQLdb
http://stackoverflow.com/questions/5669878/when-to-close-cursors-using-mysqldb I'm building a WSGI w ...
- 练习使用markdown编辑
markdown纯文本编辑语言加粗,斜体Core Java 这是一篇学习了markdown编辑之后的练习 无序列表 1 2 > 怕什么真理无穷,进一寸有一寸的欢喜! 以下是代码的引用 class ...
- PHP的PSR系列规范都有啥内容
PSR 是PHP Standard Recommendation的简写,它其实应该叫PSRs,即系列推荐标准:目前通过的规范有PSR-0(Autoloading Standard).PSR-1(Bas ...
- Ibatis中常见错误解决方案
在Ibatis 的sqlMap或者sqlMapConfig配置文件中如果出现以下错误信息: Referenced file contains errors (http://www.ibatis.com ...