1.主线程中使用
值得注意的是,CountDownTimer可以在主线程中直接使用。验证一下回调的执行线程。在主线程中执行如下代码
CountDownTimer countDownTimer = new CountDownTimer(1000L * 30, 1000L) { @Override public void onTick(long millisUntilFinished) { // your logic for tick Log.i("TEST", "onTick - " + Thread.currentThread() + ", id - " + Thread.currentThread().getId()); } @Override public void onFinish() { // your logic for finish Log.i("TEST", "onFinish" + Thread.currentThread() + ", id - " + Thread.currentThread().getId()); } }; countDownTimer.start();
Log:
02-24 14:05:13.545 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:14.546 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:15.548 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:16.550 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:17.551 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:18.553 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:19.555 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:20.557 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:21.560 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:22.563 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:23.566 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:24.569 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:25.571 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:26.573 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:27.575 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:28.578 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:29.580 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:30.581 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:31.583 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:32.586 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:33.588 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:34.589 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:35.592 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:36.595 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:37.598 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:38.599 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:39.601 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:40.602 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:41.604 30358 30358 I TEST : onTick - Thread[main,5,main], id - 1 02-24 14:05:43.546 30358 30358 I TEST : onFinishThread[main,5,main], id - 1
可见,回调执行在主线程,这也就意味着可以在回调中直接处理UI,还是很方便的。
2.在工作线程中使用
不能在工作线程中像主线程一样简单使用的原因,是CountDownTimer是基于Handler的,细节将在原理部分说明。主线程已经由安卓框架层启动了消息循环,所以可以直接使用。那么在工作线程中怎样使用呢?可以借助于HanderThread。在onLooperPrepared()中初始化并开启CountDownTimer:
new HandlerThread("TestThread") { protected void onLooperPrepared() { CountDownTimer countDownTimer = new CountDownTimer(1000L * 30, 1000L) { @Override public void onTick(long millisUntilFinished) { // your logic for tick Log.i("TEST", "onTick - " + Thread.currentThread() + ", id - " + Thread.currentThread().getId()); } @Override public void onFinish() { // your logic for finish Log.i("TEST", "onFinish" + Thread.currentThread() + ", id - " + Thread.currentThread().getId()); } }; countDownTimer.start(); } }.start();
执行log如下:
02-24 14:03:35.789 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:03:36.790 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:03:37.793 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:03:38.795 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:03:39.797 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:03:40.800 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:03:41.801 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:03:42.803 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:03:43.804 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:03:44.806 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:03:45.809 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:03:46.810 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:03:47.813 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:03:48.814 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:03:49.816 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:03:50.818 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:03:51.820 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:03:52.821 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:03:53.823 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:03:54.825 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:03:55.827 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:03:56.828 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:03:57.829 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:03:58.831 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:03:59.832 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:04:00.835 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:04:01.836 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:04:02.838 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:04:03.840 30099 30222 I TEST : onTick - Thread[TestThread,5,main], id - 473 02-24 14:04:05.791 30099 30222 I TEST : onFinishThread[TestThread,5,main], id - 473