java 中Handler 和Runnable 的使用 异步发送消息 转
public class MainActivity extends Activity {
TextView text1, text2;
Button button;
Thread th;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text1 = (TextView)findViewById(R.id.text1);
text2 = (TextView)findViewById(R.id.text2);
button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
th = new Thread(runnable);
th.start();
}
});
}
Handler myHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case 1:
System.out.println("-----------1--------------");
System.out.println(msg.getData().getString("s1"));
System.out.println(msg.getData().getString("s2"));
try{
text1.setText(msg.getData().getString("s1"));
text2.setText(msg.getData().getString("s1"));
}catch(Exception e){
e.printStackTrace();
}
break;
default:
break;
}
super.handleMessage(msg);
}
};
Runnable runnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
String s1 = "fsdfsgfdsgdfgfdgdhshshs";
String s2 = "fsfsdgdshdhdshrehreherh";
Message msg = new Message();
msg.what = 1;
Bundle bundle = new Bundle();
bundle.putString("s1", s1);
bundle.putString("s2", s2);
msg.setData(bundle);
MainActivity.this.myHandler.sendMessage(msg);
}
};
}
java 中Handler 和Runnable 的使用 异步发送消息 转的更多相关文章
- kafka7 探索生产者同步or异步发送消息
1.生产者:在发送完消息后,收到回执确认. 主要是在SimpleProducer.java中修改了发送消息的2行代码,用到了回调函数,修改如下: //发送消息 ProducerRecord<St ...
- Rocketmq异步发送消息
package com.bfxy.rocketmq.quickstart; import java.util.List; import org.apache.rocketmq.client.excep ...
- ActiveMQ producer同步/异步发送消息
http://activemq.apache.org/async-sends.html producer发送消息有同步和异步两种模式,可以通过代码配置: ((ActiveMQConnection)co ...
- java中 synchronized 的使用,确保异步执行某一段代码。
最近看了个有关访问网络url和下载的例子,里面有几个synchronized的地方,系统学习下,以下内容很重要,记下来. Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一 ...
- 增加线程异步发送消息的方法二(Runnable)
//获取当前时间:毫秒 long a = System.currentTimeMillis(); System.out.println("a :" + a); try { //更改 ...
- 增加线程异步发送消息的方法一(Thread)
@RequestMapping(value="order/updateOrder.do") public String updateOrder(HttpServletRequest ...
- Java 中的“implements Runnable” 和“extends Thread”(转)
知识点 “implements Runnable” 和“extends Thread”的不同 具体分析 最近在学习Android中的Handler消息传递机制时,创建新线程有两种方式:一种是实现Run ...
- Java 中的“implements Runnable” 和“extends Thread”
知识点 “implements Runnable” 和“extends Thread”的不同 具体分析 最近在学习Android中的Handler消息传递机制时,创建新线程有两种方式:一种是实现Run ...
- Java中使用HTTP阻塞式调用服务器API
应用场景:前端页面点击刷新,调用服务器A上Java接口,然后A调用服务器B的后台Python接口实时刷新后台数据库. 在这个场景中会涉及到两个问题:异步,Python服务器压力 (一)解决Python ...
随机推荐
- php多线程pthreads的安装与使用
安装Pthreads 基本上需要重新编译PHP,加上 --enable-maintainer-zts 参数,但是用这个文档很少:bug会很多很有很多意想不到的问题,生成环境上只能呵呵了,所以这个东西玩 ...
- FinanceJson
FinanceJson, 对Json的包装.底层使用jackson实现. 1. 生成节点 (1)在某个路径下添加某个节点 FinanceJson financeJsonInfo = FinanceJs ...
- PHP中IP地址与整型数字互相转换详解
这篇文章主要介绍了PHP中IP地址与整型数字互相转换详解,本文介绍了使用PHP函数ip2long与long2ip的使用,以及它们的BUG介绍,最后给出自己写的两个算法,需要的朋友可以参考下 IP转换成 ...
- 烂泥:【解决】ubuntu使用远程NFS报错
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 今天在ubuntu系统上使用远程NFS,发现一直报错无法使用. 查看NFS挂载命令没有错误,命令如下: mount -t nfs 192.168.1.1 ...
- Linux 之创建工作目录-mkdir
在Linux下创建工作目录,一般使用 "mkdir" 指令,一下将介绍"mkdir"指令的使用方法,供大家参考. 一.使用帮助 在Linux终端(命令行)输入: ...
- A python tool to static sim.log duration time
When working ALU IMS Patch team, we need to static the SU duration to add it to the patch report, th ...
- ArcGis 10+Oracle发布WFS-T服务,无法更新Feature的解决方法
现象: 前端采用Openlayers,更新Feature时服务器端返回的XML提示更新错误 原因: 参考:http://support.esri.com/en/knowledgebase/techar ...
- MBean的学习
参考:http://tuhaitao.iteye.com/blog/786391 这里以MBean对象进行演示向服务器注册,调用的过程. 1.MBean接口,接口名必须以MBean结尾 package ...
- 你知道的display的值有多少?用了多少?
它的语法如下: display:none | inline | block | list-item | inline-block | table | inline-table | table-capt ...
- 在mac os下的Apache服务器的cgi中运行python
我是搬运工.. Running Python Programs on the Mac OS X Apache Web Server The Mac OS X operating system incl ...