在 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.


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

利用Handler定时更新Android UI

代码

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 随手记 SQLITE代码 直接能用
  4. android draw bitmap 示例代码
  5. android edittext 显隐密码代码转换两种方式
  6. android制作一个简单登入界面的部分代码

随机推荐

  1. MySQL跨表查询与跨表更新
  2. MySQL拼接字符串函数GROUP_CONCAT详解
  3. 解决mysql5.6 utf8设置无效问题
  4. Windows10下mysql 8.0.19 winx64安装教程
  5. mysql 8.0.19 安装配置方法图文教程
  6. mysql 8.0.19 winx64.zip安装教程
  7. MySQL中使用group by 是总是出现1055的错
  8. mysql日志触发器实现代码
  9. mysql 8.0.19 win10快速安装教程
  10. CentOS7 通过YUM安装MySQL5.7的步骤详解