Handler具体解释系列(七)——Activity.runOnUiThread()方法具体解释
MainActivity例如以下:
package cc.testui3;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
/**
* Demo描写叙述:
* 在子线程中更改UI的方式三
*
* 调用Activity.runOnUiThread(Runnable 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();
* }
* }
*
*
* 源代码中的这段凝视太具体和贴心了,赞一个!
* 在UI线程中运行该Runnable.
* 假设当前线程是UI线程,那么该Runnable会马上运行.
* 假设当前的线程不是UI线程则调用UI线程handler的post()方法将其放入UI线程的消息队列中.
* 注意:勿在runOnUiThread(Runnable runnable)中做耗时操作
*
* 參考资料:
* http://blog.csdn.net/guolin_blog/article/details/9991569
* Thank you very much
*/
public class MainActivity extends Activity {
private TextView mTextView;
private Activity mActivity;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
mActivity=this;
mTextView=(TextView) findViewById(R.id.textView);
mButton=(Button) findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListenerImpl());
} private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View v) {
new Thread(){
public void run() {
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
mTextView.setText("My name is zxc , number is 007");
}
});
};
}.start();
}
} }
main.xml例如以下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
> <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dip"
/> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_centerHorizontal="true"
android:layout_marginTop="120dip"
/> <TextView
android:id="@+id/tipTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="測试在子线程中更新UI"
android:layout_centerInParent="true"
/> </RelativeLayout>
Handler具体解释系列(七)——Activity.runOnUiThread()方法具体解释的更多相关文章
- android Activity runOnUiThread() 方法使用
在android 中我们一般用 Handler 做主线程 和 子线程 之间的通信 . 现在有了一种更为简洁的写法,就是 Activity 里面的 runOnUiThread( Runnable )方法 ...
- android Activity runOnUiThread() 方法的使用
利用Activity.runOnUiThread(Runnable)把更新ui的代码创建在Runnable中,然后在需要更新ui时,把这个Runnable对象传给Activity.runOnUiThr ...
- Android中的动画具体解释系列【4】——Activity之间切换动画
前面介绍了Android中的逐帧动画和补间动画,并实现了简单的自己定义动画.这一篇我们来看看怎样将Android中的动画运用到实际开发中的一个场景--Activity之间跳转动画. 一.定义动画资源 ...
- activity.runOnUiThread()内的run()方法没有被执行
activity.runOnUiThread(new Runnable() { public void run() { Toast.makeText(context, toast, Toast.LEN ...
- Android Studio 单刷《第一行代码》系列 04 —— Activity 相关
前情提要(Previously) 本系列将使用 Android Studio 将<第一行代码>(书中讲解案例使用Eclipse)刷一遍,旨在为想入坑 Android 开发,并选择 Andr ...
- 理解Activity.runOnUiThread()
这是一篇译文(中英对照),原文链接:Understanding Activity.runOnUiThread() When developing Android applications we alw ...
- 【转】Android总结篇系列:Activity生命周期
[转]Android总结篇系列:Activity生命周期 Android官方文档和其他不少资料都对Activity生命周期进行了详细介绍,在结合资料和项目开发过程中遇到的问题,本文将对Activity ...
- iOS流布局UICollectionView系列七——三维中的球型布局
摘要: 类似标签云的球状布局,也类似与魔方的3D布局 iOS流布局UICollectionView系列七——三维中的球型布局 一.引言 通过6篇的博客,从平面上最简单的规则摆放的布局,到不规则的瀑 ...
- 理解 Activity.runOnUiThread
在开发 Android 应用的时候我们总是要记住应用主线程. 主线程非常繁忙,因为它要处理绘制UI,响应用户的交互,默认情况下执行我们写下的大部分代码. 好的开发者知道他/她需要将重负荷的任务移除到工 ...
随机推荐
- hdu 4118 dfs
题意:给n个点,每个点有一个人,有n-1条有权值的边,求所有人不在原来位置所移动的距离的和最大值.不能重复 这题的方法很有看点啊,标记为巩固题 Sample Input 1 4 1 2 3 2 3 2 ...
- 搭建Nexus本地仓库
1 下载nexus安装包 网址:http://www.sonatype.org/nexus/ 建议下载最新的版本,最新的版本支持比较新的jdk版本, 1.6 肯定是不行的,必须是1.7及其以上. ...
- 洛谷OJ U552 守墓人 线段树模板题
题目描述 Description 在一个荒凉的墓地上 有一个令人尊敬的守墓人, 他看守的墓地从来 没有被盗过, 所以人们很放心的把自己的先人的墓 安顿在他那 守墓人能看好这片墓地是必然而不是偶然... ...
- 该死的Ubuntu 16.04不自动续租DHCP的IP
BUG,这是一个BUG,参考:https://bugs.launchpad.net/ubuntu/+source/isc-dhcp/+bug/1551351,如果不自动续租IP,导致的问题就是网线灯还 ...
- Oracle问题诊断过程常用SQL
--查看临时表空间使用情况select tablespace_name ,sum(size_mb),sum(used_mb),round(sum(used_mb)/sum(size_mb),2) fr ...
- Is Usb Drive () ? DeviceIoControl, IOCTL_STORAGE_QUERY_PROPERTY
http://banderlogi.blogspot.com/2011/06/enum-drive-letters-attached-for-usb.html typedef enum _STORAG ...
- IDC门外汉-单线、双线、智能多线、BGP的区别
一.单线在此不多说,不是电信,就是网通机房,是,2005年前的机房情况: 二.双线:上般是从2004年到2008年用此方法较多,此今还有不少在用此法如下: 双线主机 有单IP和单网卡双IP地址,双网卡 ...
- 【docker】关于docker 中 镜像、容器的关系理解
例如,使用docker 拉取下来一个要用的镜像es docker pull elasticsearch:5.6.9 此时es的镜像存在与服务器上 docker images 对于你运行镜像为一个容器的 ...
- iOS Sprite Kit最新特性Physics Field虚拟物理场Swift測试
在WWDC2014上,Sprite Kit又有了非常多新的提升! 当中一个非常有意思的东西就是Physics Field!也就是物理场! 这意味着我们在Sprite kit上编写虚拟物理场的游戏将变得 ...
- Peter Norvig:学习在于挑战和重复
黄小非译注(本文来自伯乐在线):本文作者Peter Norvig目前任职于Google,其职位是研究主管(Director of Research). Peter Norvig是享誉世界的计算机科学 ...