The realization of Android development triggers the timer android timer every once in a while

Look at the pictures of the old routines:

Re-trigger every 20 seconds

In fact, the implementation is also very simple. We can do it through the timer that comes with Android natively. Let's talk about the idea:

1. First realize the android timer start trigger

2. Trigger the timer again in the method that the timer ends

package com.wyze.mercury.common.utils;

import android.os.CountDownTimer;

import com.wyze.mercury.common.TimerListener;
import com.wyze.platformkit.utils.log.WpkLogUtil;

/**
 * @author : xiayiye5
 * @date : 2021/1/8 18:10
 * 类描述 : Android定时器每隔一段时间刷新灯的状态
 */
public class CountDownTimerUtils {

    private CountDownTimer countDownTimer;

    private CountDownTimerUtils() {
    }

    public static CountDownTimerUtils getInstance() {
        return Single.COUNT_DOWN_TIMER_UTILS;
    }

    public CountDownTimerUtils setTime(final TimerListener timerListener) {
        if (null == countDownTimer) {
            countDownTimer = new CountDownTimer(20 * 1000L, 1000) {
                @Override
                public void onTick(long l) {
                    WpkLogUtil.e("定时器", l / 1000 + "");
                }

                @Override
                public void onFinish() {
                    WpkLogUtil.e("定时器", Thread.currentThread().getName());
                    //执行获取灯的状态
                    timerListener.timeFinish();
                    //继续定时
                    countDownTimer.start();
                }
            };
        }
        return this;
    }

    private static final class Single {
        private static final CountDownTimerUtils COUNT_DOWN_TIMER_UTILS = new CountDownTimerUtils();
    }

    /**
     * 开始定时
     */
    public void startTime() {
        if (null != countDownTimer) {
            countDownTimer.start();
        }
    }

    /**
     * 取消定时
     */
    public void cancelTime() {
        if (null != countDownTimer) {
            countDownTimer.cancel();
            //切记置空
            countDownTimer = null;
        }
    }
}

Note: Be sure to manage the life cycle as follows. I personally recommend that you turn on the timer method and call it in the onStart life cycle.

  @Override
    protected void onStart() {
        super.onStart();
        CountDownTimerUtils.getInstance().setTime(new TimerListener() {
            @Override
            public void timeFinish() {
                //每隔一段时间刷新灯的状态
                getIotProp();
            }
        }).startTime();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //销毁的时候一定要取消定时器避免内存泄漏
        CountDownTimerUtils.getInstance().cancelTime();
    }

    @Override
    protected void onPause() {
        super.onPause();
        CountDownTimerUtils.getInstance().cancelTime();
    }

Look at the callback interface

package com.wyze.mercury.listener;

/**
 * @author : xiayiye5
 * @date : 2021/1/8 15:17
 * 类描述 :
 */
public interface TimerListener {
    /**
     * 定时完成
     */
    void timeFinish();
}

 

Guess you like

Origin blog.csdn.net/xiayiye5/article/details/112462435