android Handler.post定时器
来源:互联网 发布:亚马逊网页是java吗 编辑:程序博客网 时间:2024/06/17 15:29
Runnable 并不一定是新开一个线程,比如下面的调用方法就是运行在UI主线程中的:
Handler mHandler=new Handler(); mHandler.post(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub } });
官方对这个方法的解释如下,注意其中的:“The runnable will be run on the user interface thread. ”
boolean android.view.View .post(Runnable action)
Causes the Runnable to be added to the message queue. The runnable will be run on the user interface thread.
Parameters:
action The Runnable that will be executed.
Returns:
Returns true if the Runnable was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting.
我们可以通过调用handler的post方法,把Runnable对象(一般是Runnable的子类)传过去;handler会在looper中调用这个Runnable的Run方法执行。
Runnable是一个接口,不是一个线程,一般线程会实现Runnable。所以如果我们使用匿名内部类是运行在UI主线程的,如果我们使用实现这个Runnable接口的线程类,则是运行在对应线程的。
final Handler mHandler=new Handler();
mHandler.post(new Runnable() {
@Override
public void run() {
updateTitle();
mHandler.postDelayed(this,1000);
}
});
private void updateTitle() {
Date date = new Date();
int hour, minute, second;
String shour, sminute, ssecond;
hour = (date.getHours() + 8) % 24;
minute = date.getMinutes();
second = date.getSeconds();
if (hour < 10) {
shour = "0" + hour;
} else {
shour = "" + hour;
}
if (minute < 10) {
sminute = "0" + minute;
} else {
sminute = "" + minute;
}
if (second < 10) {
ssecond = "0" + second;
} else {
ssecond = "" + second;
}
textView.setText("当前时间:" + shour + ":" + sminute + ":" + ssecond);
}
- android Handler.post定时器
- Android Handler循环定时器
- android Handler post sendMessage
- android Handler post sendMessage
- android Handler post sendMessage
- android Handler post sendMessage
- android Handler post sendMessage
- android Handler post sendMessage
- android多线程handler+runOnUithread+view.post+handler.post
- android多线程handler+runOnUithread+view.post+handler.post 2013-
- Android多线程之handler+runOnUithread+view.post+handler.post
- Android编程:定时器和Handler的使用
- Android Timer 定时器 与handler的配合
- Android 开发之 定时器 Handler + Runnable方式
- Android定时器之Handler的postDelyed方法
- Android编程:定时器和Handler的使用
- [Android] 利用Handler实现定时器功能
- android 定时器(Handler Timer Thread AlarmManager CountDownTimer)
- APP安全报告第九期:资讯类APP安全性极低,用户数据可能因此而泄漏!
- linux如何切换到root用户
- 要使用jQuery,要记得把改对象前面加上$,这样就变成了一个jQuery对象了,就可以使用jQuery语法了
- Hibernate系列——总结篇(九)
- 算法第十三周作业02
- android Handler.post定时器
- Python中的None与空字符(NULL)的区别
- Java 集合系列02之 Collection架构
- 实体类(VO,DO,DTO)的划分
- 我的第一篇博文,来试试水!
- web.xml
- ubuntu useradd 创建用户为三无用户
- LTspice introduction
- 常见数据挖掘算法和Python简单实现