Android 实现定时器的三种方法

本文介绍 Android 实现定时器的三种方法,分别是:方法一 Handler + Thread,方法二 Handler 自带的 postDelyed 方法,方法三 Handler + Timer + TimerTask。

方法一:Handler + Thread


  1. package com.example.qidong;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.os.Message;
  6. import android.widget.TextView;
  7. public class HanderDemoActivity extends Activity {
  8. TextView tvShow;
  9. private int i = 0;
  10. @Override
  11. public void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. tvShow = (TextView) findViewById(R.id.tv_show);
  15. new Thread(new ThreadShow()).start();
  16. }
  17. // handler 类接收数据更新 UI
  18. Handler handler = new Handler() {
  19. public void handleMessage(Message msg) {
  20. if (msg.what == 1) {
  21. tvShow.setText(Integer.toString(i++));
  22. System.out.println("receive....");
  23. }
  24. };
  25. };
  26. // 线程类发送消息给主线程
  27. class ThreadShow implements Runnable {
  28. @Override
  29. public void run() {
  30. // TODO Auto-generated method stub
  31. while (true) {
  32. try {
  33. Thread.sleep(1000);
  34. Message msg = new Message();
  35. msg.what = 1;
  36. handler.sendMessage(msg);
  37. System.out.println("send...");
  38. } catch (Exception e) {
  39. // TODO Auto-generated catch block
  40. e.printStackTrace();
  41. System.out.println("thread error...");
  42. }
  43. }
  44. }
  45. }
  46. }

方法二:Handler 自带的 postDelyed 方法


  1. package com.example.timer;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.widget.TextView;
  6. public class HanderDemoActivity extends Activity {
  7. TextView tvShow;
  8. private int i = 0;
  9. private int TIME = 1000; // 每隔1s执行
  10. @Override
  11. public void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. tvShow = (TextView) findViewById(R.id.tv_show);
  15. handler.postDelayed(runnable, TIME); // 开启定时器更新 UI
  16. }
  17. Handler handler = new Handler();
  18. Runnable runnable = new Runnable() {
  19. @Override
  20. public void run() {
  21. // handler 自带方法实现定时器
  22. try {
  23. handler.postDelayed(this, TIME);
  24. tvShow.setText(Integer.toString(i++));
  25. System.out.println("do...");
  26. } catch (Exception e) {
  27. // TODO Auto-generated catch block
  28. e.printStackTrace();
  29. System.out.println("exception...");
  30. }
  31. }
  32. };
  33. }

方法三:Handler + Timer + TimerTask


  1. package com.example.timer;
  2. import java.util.Timer;
  3. import java.util.TimerTask;
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.os.Handler;
  7. import android.os.Message;
  8. import android.widget.TextView;
  9. public class HanderDemoActivity extends Activity {
  10. TextView tvShow;
  11. private int i = 0;
  12. private int TIME = 1000;
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. tvShow = (TextView) findViewById(R.id.tv_show);
  18. timer.schedule(task, 1000, 1000); // 1s后执行task,经过1s再次执行
  19. }
  20. Handler handler = new Handler() {
  21. public void handleMessage(Message msg) {
  22. if (msg.what == 1) {
  23. tvShow.setText(Integer.toString(i++));
  24. }
  25. super.handleMessage(msg);
  26. };
  27. };
  28. Timer timer = new Timer();
  29. TimerTask task = new TimerTask() {
  30. @Override
  31. public void run() {
  32. // 定时任务发送消息
  33. Message message = new Message();
  34. message.what = 1;
  35. handler.sendMessage(message);
  36. }
  37. };
  38. }

(完)