在 Android 里定时更新 UI,通常使用的是java.util.Timer,java.util.TimerTask,android.os.Handler组合,这里有相关的讨论。但实际上 Handler 自身已经提供了定时的功能。

参考 android.os.Handler 的文档

引用
There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future;and (2) to enqueue an action to be performed on a different thread than your own.


Scheduling messages is accomplished with the post(Runnable), postAtTime(Runnable, long), postDelayed(Runnable, long), sendEmptyMessage(int), sendMessage(Message), sendMessageAtTime(Message, long), and sendMessageDelayed(Message, long) methods. The post versions allow you to enqueue Runnable objects to be called by the message queue when they are received; the sendMessage versions allow you to enqueue a Message object containing a bundle of data that will be processed by the Handler's handleMessage(Message) method (requiring that you implement a subclass of Handler).


When posting or sending to a Handler, you can either allow the item to be processed as soon as the message queue is ready to do so, or specify a delay before it gets processed or absolute time for it to be processed. The latter two allow you to implement timeouts, ticks, and other timing-based behavior.


下面是一个简单的计数器程序,每隔一秒递增计数器



代码

main.xml
--------
Xml代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <TextViewandroid:id="@+id/counter"android:layout_width="fill_parent"
  6. android:layout_height="wrap_content"android:text="Count:0"/>
  7. <LinearLayoutandroid:orientation="horizontal"
  8. android:layout_width="fill_parent"android:layout_height="wrap_content">
  9. <Buttonandroid:text="start"android:id="@+id/Button01"
  10. android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1.0"></Button>
  11. <Buttonandroid:text="stop"android:id="@+id/Button02"
  12. android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1.0"android:enabled="false"></Button>
  13. <Buttonandroid:text="reset"android:id="@+id/Button03"
  14. android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1.0"></Button>
  15. </LinearLayout>
  16. </LinearLayout>


TestTimer.java
--------------
Java代码
  1. packagecn.yo2.aquarium.android.testtimer;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.os.Handler;
  5. importandroid.view.View;
  6. importandroid.view.View.OnClickListener;
  7. importandroid.widget.Button;
  8. importandroid.widget.TextView;
  9. publicclassTestTimerextendsActivity{
  10. privateButtonbtnStart;
  11. privateButtonbtnStop;
  12. privateButtonbtnReset;
  13. privateTextViewtvCounter;
  14. privatelongcount=0;
  15. privatebooleanrun=false;
  16. privateHandlerhandler=newHandler();
  17. privateRunnabletask=newRunnable(){
  18. publicvoidrun(){
  19. //TODOAuto-generatedmethodstub
  20. if(run){
  21. handler.postDelayed(this,1000);
  22. count++;
  23. }
  24. tvCounter.setText("Count:"+count);
  25. }
  26. };
  27. /**Calledwhentheactivityisfirstcreated.*/
  28. @Override
  29. publicvoidonCreate(BundlesavedInstanceState){
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.main);
  32. btnStart=(Button)findViewById(R.id.Button01);
  33. btnStop=(Button)findViewById(R.id.Button02);
  34. btnReset=(Button)findViewById(R.id.Button03);
  35. tvCounter=(TextView)findViewById(R.id.counter);
  36. btnStart.setOnClickListener(newOnClickListener(){
  37. publicvoidonClick(Viewv){
  38. //TODOAuto-generatedmethodstub
  39. run=true;
  40. updateButton();
  41. handler.postDelayed(task,1000);
  42. }
  43. });
  44. btnStop.setOnClickListener(newOnClickListener(){
  45. publicvoidonClick(Viewv){
  46. //TODOAuto-generatedmethodstub
  47. run=false;
  48. updateButton();
  49. handler.post(task);
  50. }
  51. });
  52. btnReset.setOnClickListener(newOnClickListener(){
  53. publicvoidonClick(Viewv){
  54. //TODOAuto-generatedmethodstub
  55. count=0;
  56. run=false;
  57. updateButton();
  58. handler.post(task);
  59. }
  60. });
  61. }
  62. privatevoidupdateButton(){
  63. btnStart.setEnabled(!run);
  64. btnStop.setEnabled(run);
  65. }
  66. }

更多相关文章

  1. 搭建 android 代码镜像服务
  2. android源代码下载
  3. android 单选框
  4. Android(安卓)zxing change orientation to portrait
  5. How To Open An URL In Android’s Web Browser
  6. Android也有beacon了
  7. 利用Handler来更新android的UI
  8. 系出名门Android(8) - 控件(View)之TextSwitcher, Gallery, Imag
  9. Android(安卓)旋转屏幕捕获当前屏幕的状态

随机推荐

  1. Task和Activity相关-转帖
  2. android 手势识别(一)
  3. Android SqlLite的简单实用
  4. Android 短信验证码自动填写
  5. android SDK安装代理设置
  6. Jquery 判断是否 移动设备 浏览
  7. 关于build-tools 26.0.0报错解决及Neon3
  8. android 弹出对话框
  9. Android的前世今生
  10. Android(安卓)核心分析 之六 -----IPC框