Hander----使用
public class MainActivity extends Activity {
private EditText UITxt;
private Button updateUIBtn;
private UIHandler uIhandler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UITxt = (EditText)findViewById(R.id.ui_txt);
updateUIBtn = (Button)findViewById(R.id.update_ui_btn);
updateUIBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
uIhandler= new UIHandler(); // 创建Handler 对象
UIThread thread = new UIThread();
thread.start(); //
}
});
}
private class UIHandler extends Handler{
@Override
public void handleMessage(Message msg) { // 2 .主线程更新界面
super.handleMessage(msg);
Bundle bundle = msg.getData();
String color = bundle.getString("color");
UITxt.setText(color);
// if(msg.what == SUCCESS) { // 当前是访问网络, 去显示图片
// ivIcon.setImageBitmap((Bitmap) msg.obj); // 设置imageView显示的图片
// } else if(msg.what == ERROR) {
// Toast.makeText(MainActivity.this, "抓去失败", 0).show();
// }
}
}
// 子线程 中运行
private class UIThread extends Thread{
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Message msg = new Message();
// msg.what = SUCCESS;
// msg.obj = bitmap;
Bundle bundle = new Bundle();
bundle.putString("color", "黄色");
msg.setData(bundle);
MainActivity.this.uIhandler.sendMessage(msg); //1 . 子线程发消息
}
}
}
Hander----使用的更多相关文章
- android学习日记25--ANR和Hander消息机制
1.ANR(Application Not Responding)定义 在Android上,如果你的应用程序有一段时间响应不够灵敏,系统会向用户显示一个对话框,这个对话框称作应用程序无响应(ANR:A ...
- Handler 、 Looper 、Message异步消息处理线程机制( hander消息机制原理)
Handler . Looper .Message 这三者都与Android异步消息处理线程相关的概念. 那么什么叫异步消息处理线程呢? 异步消息处理线程启动后会进入一个无限的循环体之中,每循环一次, ...
- Android Handler机制 (一个Thead中可以建立多个Hander,通过msg.target保证MessageQueue中的每个msg交由发送message的handler进行处理 ,但是 每个线程中最多只有一个Looper,肯定也就一个MessageQuque)
转载自http://blog.csdn.net/stonecao/article/details/6417364 在android中提供了一种异步回调机制Handler,使用它,我们可以在完成一个很长 ...
- android hander
http://www.cnblogs.com/plokmju/p/android_Handler.html 前言 Android的消息传递机制是另外一种形式的“事件处理”,这种机制主要是为了解决And ...
- hander消息机制原理
基本原理 线程中调用Handler.sendMsg()方法(参数是Message对象),将需要Main线程处理的事件 添加到Main线程的MessageQueue中,Main线程通过MainLoope ...
- 通过Hander进行界面刷新
Timer timer; TimerTask task; Handler handler;//先声明这3个变量 //在onCreate方法内 handler = new Handler(){ @Ove ...
- Hander
多线程与UI线程间通信 向你展示如何从任务发送数据对象上运行用户界面(UI)线程.该特性允许你的任务做背景的工作结果,然后再到UI元素如位图. 每个应用程序都有自己的特殊的线程运行的UI对象如视图对象 ...
- android hander 线程用法
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanc ...
- Looper、Hander、HandlerThread
一.Message .Looper.Handler之间的关系 1.系统发送的Message消息传送给Handler,Handler将Message放入自己的looper队列的底部 然后再从Loop ...
- hander消息机制原理(looper轮询监听机制)
基本原理 线程中调用Handler.sendMsg()方法(参数是Message对象),将需要Main线程处理的事件 添加到Main线程的MessageQueue中,Main线程通过MainLoope ...
随机推荐
- UDP标准模型
伪代码 #服务端 #创建UDP服务器 ss = socket() #创建一个服务器套接字 ss.bind() #绑定服务器套接字 inf_loop: #服务器无限循环 cs = ss.recvfrom ...
- Swift协议+代理
Swift语言开发中使用协议+代理的用法和oc中是一样的,只不过变得是语法.现在就进入swift的协议+代理. 先上个图,看看我们要实现的效果: 首先是第一个页面,然后点击到第二个页面,最后点击返回 ...
- 【leetcode刷题笔记】Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- mysql 数据库备份方案及策略
由于mysql存在多种数据库备份方式,而且各有利弊,对于我们初学者来说,选择合适的备份方式确实有些困难.个人觉得,首先要基于公司的需求,考虑能够容忍丢失多少数据.花多少人力时间成本等,这是我们制定备份 ...
- hbase shell-ddl(表定义指令)
hbase表定义指令详细解说篇 1. alter, alter_async, alter_status 2. create 3. describe (可以简写成'desc') 显示某张表的结构情况 ...
- Android 4.4 U盘挂载【转】
本文转载自:https://blog.csdn.net/mill_li/article/details/50134301 在Android4.4系统下,ES文件浏览器读取不到U盘,但是在设置-> ...
- Linux内核源码中的likely和unlikely释疑【转】
本文转载自:https://my.oschina.net/armsky/blog/15320 ikely()与unlikely()在2.6内核中,随处可见,那为什么要用它们?它们之间有什么区别呢? 首 ...
- [算法]在数组中找到出现次数大于N/K的数
题目: 1.给定一个整型数组,打印其中出现次数大于一半的数.如果没有出现这样的数,打印提示信息. 如:1,2,1输出1. 1,2,3输出no such number. 2.给定一个整型数组,再给 ...
- <linux硬件及硬盘分区>关于硬盘的规划和使用细节
ps:期末考试 终于结束了,这下我也终于有时间开始继续经营我的博客.这个学期上的一些课真的非常有用,感觉很多课程细地讲都可以写成非常精致的技术博文,比如流水线技术,数据库的一些技术,大学里的考试考的内 ...
- HYSBZ - 1588 splay
题意:每天给你一个数,要求统计最小波动值,强制在线的就是每次从已经出现过的数值中找与当前值间隔最小的加起来 题解:splay维护,同时求解当前值的前驱和后继,找距离小的那个就好了 splay是一种二叉 ...