Android定时器实现定时执行、重复执行、定时重复执行、定次数执行的多种方式

  package com.example.leixiansheng.mytimers;

  import android.os.Handler;

  import android.os.Message;

  import android.support.v7.app.AppCompatActivity;

  import android.os.Bundle;

  import android.view.View;

  import android.widget.Button;

  import java.util.Timer;

  import java.util.TimerTask;

  public class MainActivity extends AppCompatActivity implements View.OnClickListener {

  private Button timer_1, timer_2, timer_3, timer_4,clear, showTime;

  private Timer timer;

  private TimerTask timerTask;

  private int num = 0; //计数值

  private boolean flog = true; //是否停止计时

  private Handler handler = new Handler() {

  @Override

  public void handleMessage(Message msg) {

  super.handleMessage(msg);

  showTime.setText("点击我停止计时: " + msg.what);

  }

  };

  //handler 方式定时循环

  private Handler handlerTimer = new Handler() {

  @Override

  public void handleMessage(Message msg) {

  super.handleMessage(msg);

  if (flog) {

  handlerTimer.sendEmptyMessageDelayed(num++, 1000);

  }

  showTime.setText("点击我停止计时: " + msg.what);

  if(flog == false) {

  flog = true;

  }

  }

  };

  @Override

  protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

  timer_1 = (Button) findViewById(R.id.timer_1);

  timer_2 = (Button) findViewById(R.id.timer_2);

  timer_3 = (Button) findViewById(R.id.timer_3);

  timer_4 = (Button) findViewById(R.id.timer_4);

  clear = (Button) findViewById(R.id.clear);

  showTime = (Button) findViewById(R.id.show_time);

  timer_1.setOnClickListener(this);

  timer_2.setOnClickListener(this);

  timer_3.setOnClickListener(this);

  timer_4.setOnClickListener(this);

  clear.setOnClickListener(this);

  showTime.setOnClickListener(this);

  }

  @Override

  public void onClick(View view) {

  switch (view.getId()) {

  case R.id.timer_1:

  method_1();

  break;

  case R.id.timer_2:

  method_2();

  break;

  case R.id.timer_3:

  method_3();

  break;

  case R.id.timer_4:

  method_4();

  break;

  case R.id.clear:

  num = 0;

  showTime.setText("请选择一种启动方式");

  break;

  case R.id.show_time:

  flog = false;

  break;

  }

  }

  private void method_4() {

  new Thread() {

  @Override

  public void run() {

  super.run();

  handlerTimer.sendEmptyMessage(num++);

  }

  }.start();

  }

  private void method_3() {

  //Timer一旦被cancel之后就废了,只有重新构造一个。

  if (flog == true) {

  timer = new Timer();

  timerTask = new TimerTask() {

  @Override

  public void run() {

  handler.sendEmptyMessage(num++);

  }

  };

  /**

  * 第一个参数:任务

  * 第二个参数:初始启动等待时间

  * 第三个参数:时间间隔

  */

  timer.schedule(timerTask, 0, 1000);

  } else {

  timer.cancel();

  // 一定设置为null,否则定时器不会被回收

  timer = null;

  flog = true;

  }

  //Timer 暂停重启有问题(待改进)

  //Timer一旦被cancel之后就废了,只有重新构造一个。

  // if (flog == true) {

  // timerTask = new TimerTask() {

  // @Override

  // public void run() {

  // handler.sendEmptyMessage(num++);

  // }

  // };

  // /**

  // * 第一个参数:任务

  // * 第二个参数:初始启动等待时间

  // * 第三个参数:时间间隔

  // */

  // timer.schedule(timerTask, 0, 1000);

  // } else {

  // timer.cancel();

  // flog = true;

  // }

  }

  private void method_2() {

  //new Thread().start();

  new Thread() {

  @Override

  public void run() {

  super.run();

  while (flog) {

  handler.sendEmptyMessage(num++);

  try {

  Thread.sleep(1000);

  } catch (InterruptedException e) {

  e.printStackTrace();

  }

  }

  }

  }.start();

  flog = true;

  }

  private void method_1() {

  // new Thread(new Runnable).start();

  new Thread(new Runnable() {

  @Override

  public void run() {

  while (flog) {

  handler.sendEmptyMessage(num++);

  try {

  Thread.sleep(1000);

  } catch (InterruptedException e) {

  e.printStackTrace();

  }

  }

  }

  }).start();

  flog = true;

  }

  }