【问题标题】:Android countdown timer gradually losing timeAndroid倒数计时器逐渐失去时间
【发布时间】:2012-03-04 09:15:38
【问题描述】:

我正在尝试创建一个针对平板电脑优化的倒数计时器,它运行良好,除了随着时间的推移它逐渐失去时间的微小细节:在我的 Xoom 上测试时,超过 5 分钟它可能会失去 1-2 秒.

这是我的 onTick 代码:

        public void onTick(long millisUntilFinished) {
        // TODO Auto-generated method stub
        currentTime = System.currentTimeMillis();
        currentCount = startTime - currentTime;
        //currentCount = millisUntilFinished;
        displayCorrectTime();
    }

一开始我只使用“millisUntilFinished”,但我注意到时间在流逝。然后,我阅读并尝试通过使用开始时间和“现在”之间的差异来通知计时器,让它更好地同步。不幸的是,这两种方法似乎都浪费了时间(我不太明白第二种方法怎么会浪费时间,但是)。

有什么建议吗?

【问题讨论】:

标签: android timer countdown


【解决方案1】:

这很正常,System.currentTimeMillis() 有这种不精确性。您可以尝试System.nanoTime() 以获得(理论上的)更好的精度。无论如何,您需要补偿执行 displayCorrectTime() 调用所花费的时间。

更好的解决方案是使用:

http://developer.android.com/reference/java/util/Timer.html

【讨论】:

  • hmm .. 还没有遇到计时器(我是菜鸟).. 有一些阅读要做。另外,“补偿”是什么意思?
  • 您希望方法 displayCorrectTime() 每隔 300 毫秒(比方说)执行一次。每次调用该方法调用的时间都会略有不同(有时可能是 10 毫秒,有时是 23 毫秒)。因此,要始终在上次调用后 300 毫秒后调用它,您应该测量两次调用之间经过了多少时间(就像您现在所做的那样)以及最后一次方法调用花费了多少时间。当时间超过 MINUS 调用时间过去 300 毫秒时,是时候再次调用该方法了。查看有关如何创建游戏循环的一些基础知识:p-xr.com/…
【解决方案2】:
new CountDownTimer(60000, 1000) { 
        long startTime = System.currentTimeMillis();
        public void onTick(long millisUntilFinished) {
            long timePassed = System.currentTimeMillis() - startTime;
            pb.setProgress(60000-(int)timePassed);
            t.setText(String.valueOf((60000-timePassed)/1000));
        }

        public void onFinish() {
            Intent intent = new Intent(GameActivity.this, SummaryActivity.class);
            startActivity(intent);
        }
    }.start();

这对我有用。这是一个带有进度条显示的 1 分钟倒计时计时器。

【讨论】:

    【解决方案3】:

    这是我很久以前为 Java 服务器应用程序编写的一个简单函数:

    /// <summary>
    /// A static helper class the enables a thread to sleep for a specific period of
    /// time, manage drift and supports clock changes.
    /// </summary>
    public static class ThreadHelper
    {
        /// <summary>
        /// Enables a thread to sleep for a specific period of time, manage drift and
        /// supports clock changes. This function enables the user to break from the
        /// call.
        /// </summary>
        /// <param name="startTicks">A long value that specifies the time to start sleeping.</param>
        /// <param name="intervalTicks">The long value that specifies the duration to sleep for.</param>
        /// <param name="signalStop">A reference to a bool that enables the caller to
        /// break out of this call from another thread.</param>
        /// <returns>A long value that specifies the value the caller should use for the
        /// startTicks parameters on subsequent calls into this function.</returns>
        public static long Sleep(long startTicks, long intervalTicks, ref bool signalStop)
        {
            long finalTicks = startTicks + intervalTicks;
    
            while (finalTicks + intervalTicks < DateTime.Now.Ticks)
            {
                finalTicks += intervalTicks;
            }
    
            while (!signalStop && DateTime.Now.Ticks < finalTicks)
            {
                while (finalTicks - intervalTicks > DateTime.Now.Ticks)
                {
                    finalTicks -= intervalTicks;
                }
    
                Thread.Sleep(1000);
            }
            return finalTicks;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多