Android计时器TimerTask,Timer,若要在TimerTask中更新主线程UI,鉴于Android编程模型不允许在非主线程中更新主线程UI,因此需要结合Android的Handler实现在Java的TimerTask中更新主线程UI。
现给出一个简单示例。代码使用标准Java的TimerTask和Timer启动一个计时器Task。该任务每隔2秒更新主线程的UI(在主线程的TextView显示最新的系统时间:System.currentTimeMillis())。

 import java.util.Timer;
import java.util.TimerTask; import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.widget.TextView; public class MainActivity extends Activity { private Timer timer;
private TimerTask task; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); final TextView tv = (TextView) findViewById(R.id.textView); final int WHAT = 102;
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case WHAT:
tv.setText(msg.obj + "");
break;
}
}
}; task = new TimerTask() {
@Override
public void run() {
Message message = new Message();
message.what = WHAT;
message.obj = System.currentTimeMillis();
handler.sendMessage(message);
}
}; timer = new Timer();
// 参数:
// 1000,延时1秒后执行。
// 2000,每隔2秒执行1次task。
timer.schedule(task, 1000, 2000);
} @Override
protected void onStop() {
super.onStop(); // 暂停
// timer.cancel();
// task.cancel();
}
}

Android计时器TimerTask,Timer,Handler的更多相关文章

  1. 115、定时器(TimerTask+Timer+Handler)

    public class TimerUtils { public static Activity act; public static List<MaiDianModels> listMa ...

  2. Android简易实战教程--第四十八话《Android - Timer、TimerTask和Handler实现倒计时》

    之前本专栏文章中的小案例有写到:第三十九话<Chronometer实现倒计时> 以及使用异步实现倒计时:第三十三话< AsyncTask异步倒计时> 本篇文章 结合Timer. ...

  3. Android计时器和倒计时

    Android计时器和倒计时 计时器两个核心类 Timer 和 TimerTask 1) Timer核心方法 Java代码  //Schedules the specified task for ex ...

  4. 倒计时 总结 Timer Handler CountDownTimer RxJava MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  5. Android计时器实例

    布局文件 <Chronometer android:id="@+id/chronometer" android:layout_width="wrap_content ...

  6. iOS中的NSTimer 和 Android 中的Timer

    首先看iOS的, Scheduling Timers in Run Loops A timer object can be registered in only one run loop at a t ...

  7. android 多线程Thread,Runnable,Handler,AsyncTask

    先看两个链接: 1.http://www.2cto.com/kf/201404/290494.html 2. 链接1: android 的多线程实际上就是java的多线程.android的UI线程又称 ...

  8. 解决UITableView上计时器(Timer)的滑动问题

    要想计时器(Timer)不因UITableView的滑动而停止工作,就得探讨一下RunLoop了. RunLoop本质和它的意思一样是运行着的循环,更确切的说是线程中的循环.它用来接受循环中的事件和安 ...

  9. 【Android 开发】: Android 消息处理机制之一: Handler 与 Message

    最近几讲内容,我们学习了Android中关于多线程的一些知识,上一讲我们讲解了异步任务 AsyncTask 的操作,Android中还提供了其他的线程操作,如Handler Message Messa ...

随机推荐

  1. C# 中将多个空格替换成一个空格

    2013-04-17 15:36 C# 中如何将多个空格替换成一个空格? 1 myString = Regex.Replace(myString, @"\s+", " & ...

  2. (easy)LeetCode 234.Palindrome Linked List

    Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...

  3. CCParticleSystem粒子系统

    欢迎转载!转载时请注明出处:http://write.blog.csdn.net/postedit/8124781 第一次接触粒子系统,以前游戏里面的一些小特效,像制作动画一样,是采用一帧一帧的切出来 ...

  4. [HDU 4747] Mex (线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4747 这道题是我去年刚入校队的时候参加网赛的题目. 一年过去了,我依然还是不会做.. 这是我难题计划的 ...

  5. 推荐一个代码生成工具:freemarker

    freemarker:http://freemarker.org/ 还有velocity:http://velocity.apache.org/

  6. IntelliJ IDEA调整控制台输出字体大小

    File->Settings->Editor->Colors & Fonts->Console Font Size: 16

  7. String 去重,区分大小写

    题目要求:去除,和.,相同的单词去除后面的.区分大小写 示例:输入:There is a will,there is a way. 输出There is a will there way 答案代码: ...

  8. Unity AssetBundles and Resources指引 (四) AssetBundle使用模式

    本文内容主要翻译自下面这篇文章 https://unity3d.com/cn/learn/tutorials/topics/best-practices/guide-assetbundles-and- ...

  9. 怎样用VB编写.DLL动态链接库文件

    VB一般可以生成两种特殊的DLL,一个是ActiveX DLL和ActiveX Control(*.ocx).这两种DLL都是VB支持的标准类型,在VB自身的例子中有,你可以参考.更详细的介绍可以参考 ...

  10. NSInteger 和 int 区别

      #if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_3 ...