CustomCountDownTimer解决计时不准问题

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ThugKd/article/details/80583591

项目中使用到了倒计时,在git上找到了https://github.com/iwgang/CountdownView。但是发现在项目中使用的时候出现倒计时不准的问题,后来自己在原来基础上进行了改写,除去了代码中SystemClock.elapsedRealtime();不会出现倒计时不准的问题。

public abstract class CustomCountDownTimer {
    private static final int MSG = 1;
    private final long mMillisInFuture;
    private final long mCountdownInterval;
    private long mStopTimeInFuture;
    private long mPauseTimeInFuture;
    private boolean isStop = false;
    private boolean isPause = false;

    /**
     * @param millisInFuture    总倒计时时间
     * @param countDownInterval 倒计时间隔时间
     */
    public CustomCountDownTimer(long millisInFuture, long countDownInterval) {
        mMillisInFuture = millisInFuture;
        mCountdownInterval = countDownInterval;
    }

    private synchronized CustomCountDownTimer start(long millisInFuture) {
        isStop = false;
        if (millisInFuture <= 0) {
            onFinish();
            return this;
        }
        mStopTimeInFuture = millisInFuture;
        mHandler.sendMessage(mHandler.obtainMessage(MSG));
        return this;
    }

    /**
     * 开始倒计时
     */
    public synchronized final void start() {
        start(mMillisInFuture);
    }

    /**
     * 停止倒计时
     */
    public synchronized final void stop() {
        isStop = true;
        mHandler.removeMessages(MSG);
    }

    /**
     * 暂时倒计时
     * 调用{@link #restart()}方法重新开始
     */
    public synchronized final void pause() {
        if (isStop) {
            return;
        }

        isPause = true;
        mPauseTimeInFuture = mStopTimeInFuture;
        mHandler.removeMessages(MSG);
    }

    /**
     * 重新开始
     */
    public synchronized final void restart() {
        if (isStop || !isPause) {
            return;
        }

        isPause = false;
        start(mPauseTimeInFuture);
    }

    /**
     * 倒计时间隔回调
     *
     * @param millisUntilFinished 剩余毫秒数
     */
    public abstract void onTick(long millisUntilFinished);

    /**
     * 倒计时结束回调
     */
    public abstract void onFinish();

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            synchronized (CustomCountDownTimer.this) {
                if (isStop || isPause) {
                    return;
                }

                final long millisLeft = mStopTimeInFuture - mCountdownInterval;
                if (millisLeft <= 0) {
                    onFinish();
                } else {
                    onTick(millisLeft);
                    mStopTimeInFuture = millisLeft;
                    sendMessageDelayed(obtainMessage(MSG), mCountdownInterval);
                }
            }
        }
    };
}
展开阅读全文

settimer问题不准

07-18

void CTimeDlg::On_start() //按钮,获取GPS时间,并且修改电脑时间rnrnrn char cBuffer1[2048];rn CString ss;rn rnchar tim[9];rn int i,j;rnfor(j=0;j<9;j++)rntim[j] =0;rnrn UpdateData(true);rnrn ReadComm( cBuffer1,2048); //串口读数据rn for(i=0;i<2048;i++)rn rn if(cBuffer1[i] =='$')rn if(cBuffer1[i+1] == 'G')rn if(cBuffer1[i+2] == 'P')rn if(cBuffer1[i+3] == 'G')rn if(cBuffer1[i+4] == 'G')rn for(j=0;j<9;j++)rn tim[j]=cBuffer1[i+j+7];rn rn rn int zlf[4];rnzlf[0] =(int) ((tim[0]-0x30)*10+tim[1]-0x30);rnzlf[1] =(int) ((tim[2]-0x30)*10+tim[3]-0x30);rnzlf[2] = (int)((tim[4]-0x30)*10+tim[5]-0x30);rn rnrnSYSTEMTIME NowTime;rn rn SYSTEMTIME tt; rn GetSystemTime(&NowTime);rn tt.wDay=NowTime.wDay; rn tt.wHour=zlf[0];rn tt.wMilliseconds=0; rn tt.wMinute=zlf[1]; rn tt.wMonth=NowTime.wMonth; rn tt.wSecond=zlf[2]; rn tt.wYear=NowTime.wYear; rn rn SetSystemTime(&tt); rn rn ss.Format("%2d:%2d:%2d",tt.wHour+8,tt.wMinute,tt.wSecond) ;rn if(tt.wHour<33)rn rnUpdateData(true);rnm_time=ss; //文本框显示时间rnUpdateData(false); rn rnrn SetTimer(1,60000,NULL); //1分钟触发定时器rnrnrnvoid CTimeDlg::OnTimer(UINT nIDEvent) //响应函数rnrnchar cBuffer2[2048];rn CString ss;rn rnchar tim[9];rn int i,j;rn UpdateData(true);rn if (nIDEvent==1)rn rn for(j=0;j<9;j++)rn tim[j] =0;rn for(j=0;j<9;j++)rn rn ReadComm( cBuffer2,2048);rn //Sleep(10);rn rnrn for(i=0;i<2048;i++)rn rn if(cBuffer2[i] =='$')rn if(cBuffer2[i+1] == 'G')rn if(cBuffer2[i+2] == 'P')rn if(cBuffer2[i+3] == 'G')rn if(cBuffer2[i+4] == 'G')rn for(j=0;j<9;j++)rn tim[j]=cBuffer2[i+j+7];rn rn rn int zlf[4];rn zlf[0] =(int) ((tim[0]-0x30)*10+tim[1]-0x30);rn zlf[1] =(int) ((tim[2]-0x30)*10+tim[3]-0x30);rn zlf[2] = (int)((tim[4]-0x30)*10+tim[5]-0x30);rn rnrn SYSTEMTIME NowTime;rn rn SYSTEMTIME tt; rn GetSystemTime(&NowTime);rn tt.wDay=NowTime.wDay; rn tt.wHour=zlf[0];rn tt.wMilliseconds=0; rn tt.wMinute=zlf[1]; rn tt.wMonth=NowTime.wMonth; rn tt.wSecond=zlf[2]; rn tt.wYear=NowTime.wYear; rn rn SetSystemTime(&tt); rn rn ss.Format("%2d:%2d:%2d",tt.wHour+8,tt.wMinute,tt.wSecond) ;rn if(tt.wHour<33)rn rn rnm_time=ss;rnUpdateData(false); rn rn rnUpdateData(FALSE);rnrn CDialog::OnTimer(nIDEvent);rnrnrn时间不准,GPS获取时间有时候也不对 论坛

没有更多推荐了,返回首页