有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java计时器总是在时间的前面跳跃

我实现了如下2个updateTimerThreads:

private Runnable updateTimerThread = new Runnable() {

        public void run() {

            timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

            updatedTime = timeSwapBuff + timeInMilliseconds;

            secs = (int) (updatedTime / 1000);
            mins = secs / 60;
            secs = secs % 60;
            milliseconds = (int) (updatedTime % 1000);
            //Log.d(TAG, "Secs: " + secs + " Min: " +  mins);
            customHandler.postDelayed(this, 0);
        }
    };

2种启动方式:

public void startTimer(){
        if(firsttime){
            startTime = SystemClock.uptimeMillis();
            customHandler.postDelayed(updateTimerThread, 0);
            firsttime = false;
        }
    }

和2种暂停方法:

public void pauseTimer(){
        timeSwapBuff += timeInMilliseconds;
        customHandler.removeCallbacks(updateTimerThread);
        firsttime = true;
    }

我的问题:

每当我在一个计时器和另一个计时器之间切换(暂停一个,启动另一个),它们都会不断增加大量的时间,以秒为单位的分钟值

有没有办法解决这个问题

多谢各位


共 (0) 个答案