机器人CountDownTimer示出1两秒钟 [英] Android CountDownTimer shows 1 for two seconds

查看:138
本文介绍了机器人CountDownTimer示出1两秒钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/4824068/how-come-millisuntilfinished-cannot-detect-exact-countdowntimer-intervals">How来millisUntilFinished无法检测准确CountDownTimer间隔?

为什么使用 CountDownTimer 显示1的两倍?我只是希望它平稳地倒计时,而不是像它是越来越挂在最后一秒。任何人对如何解决这个问题?任何想法

Why does using the CountDownTimer show "1" twice? I just want it to countdown smoothly and not look like it is getting hung up on the last second. Anybody have any ideas on how to solve this problem??

下面是从Android开发者页面的code:

Here is the code from the Android Developers page:

  new CountdownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {
         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     }

     public void onFinish() {
         mTextField.setText("done!");
     }
  }.start();

我创建了一个新项目,并复制和粘贴此code只是为了确保我不小心搞乱的东西在我的计划。我一直在测试这一个选项卡10.1。它这样做的时候我运行它:5,4,3,2,1,1,!做

I created a new project and copied and pasted this code in just to make sure that I wasn't accidentally messing something up in my program. I've been testing this on a Tab 10.1. It does this when I run it: "5", "4", "3", "2", "1", "1", "done!".

推荐答案

该CountDownTimer不是precise,它会返回接近1秒(在这种情况下),因为它可以,但通常这是不够的你想要的东西给你。

The CountDownTimer is not precise, it will return as close to 1 second (in this case) as it can but often that won't be enough to give you what you want.

如果你登录的MS VAR,你会看到的结果是类似29384的第一击,等等,它永远不会是30000,29000等,有没有办法让你需要的precision让柜台工作,你打算作为当前实现。

If you Log the ms var you will see that the result is something like 29384 for the first hit, and so on, it will never be 30000, 29000 etc. There is no way to get the precision you need to make the counter work as you intended as currently implemented.

您可以解决此问题在大多数情况下。首先降低你的时间间隔,让你获得比更多的更新你所想要的输出,所以不是1秒做100毫秒和1秒的倒数计时器,应该让你的幻想,因为你会得到至少每秒一些更新(最差情况下)

You can fix this for most cases. First lower you interval so that you get more updates than what you want to output, so instead of 1 second do 100ms and that should get you the illusions of a 1 second countdown timer since you will get at least a few updates per second (worst case)

要优化该位轮的每个条目的结果,并做了简单的检查,看看你应该做的更新(secondsLeft发生了变化),如果有更新UI和secondsLeft累加器。此外,我认为,onFinish产生的最终结果(0),以防万一系统故障,因为有事情发生了吃的CPU周期。

To optimize that bit round the result on each entry and do a simple check to see if you should do the update (secondsLeft has changed) and if it has update the UI and the secondsLeft accumulator. Also I suggest that onFinish produce the final result (0) just in case the system glitches because something is happening that eats cpu cycles.

反正这里是修改code如果没有加载您的系统大量的作品为这个99.99倍出的100。注意:你也许可以蒙混过关250毫秒,由你真的。

Anyway here is the modified code that works for this 99.99 times out of a 100 if your system is not loaded heavily. NOTE: You can probably get away with 250ms, up to you really.

 int secondsLeft = 0; 

 new CountDownTimer(30000, 100) {
     public void onTick(long ms) {
         if (Math.round((float)ms / 1000.0f) != secondsLeft)
         {  
             secondsLeft = Math.round((float)ms / 1000.0f);
             TV.setText("seconds remaining: " +secondsLeft );
         }
         Log.i("test","ms="+ms+" till finished="+secondsLeft );
     }

     public void onFinish() {
         TV.setText("0");
     }
}.start();

这篇关于机器人CountDownTimer示出1两秒钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆