Android:CountDownTimer跳过最后的onTick()!

Pro*_*eQQ 46 java android timer countdown countdowntimer

码:

public class SMH extends Activity {  

    public void onCreate(Bundle b) {  
        super.onCreate(b);  
        setContentView(R.layout.main);  

        TextView tv = (TextView) findViewById(R.id.tv);  

        new CountDownTimer(10000, 2000) {  
            public void onTick(long m) {  
               long sec = m/1000+1;  
               tv.append(sec+" seconds remain\n");  
            }  
            public void onFinish() {  
               tv.append("Done!");  
            }  
        }.start();  
   }
Run Code Online (Sandbox Code Playgroud)

输出:
10秒保持
8秒保持
6秒保持
4秒保持
完成!

问题:

如何让它显示" 2秒仍然 "?经过的时间确实是10秒,但最后一次onTick()从未发生过.如果我将第二个参数从2000更改为1000,那么这是输出:

10秒保持
9秒保持
8秒保持
7秒保持
6秒保持
5秒保持
4秒保持
3秒保持
2秒保持
完成!

所以你看,它似乎正在跳过最后一次onTick()调用.顺便说一句,XML文件基本上是默认的main.xml,TextView分配了id tv,文本设置为"".

Nan*_*oka 53

我检查了CountDownTimer的源代码."缺失的滴答"来自CountDownTimer的一个特殊功能,我还没有看到其他地方记录:

在每次打勾开始时,在调用onTick()之前,计算倒计时结束前的剩余时间.如果此时间小于倒计时时间间隔,则不再调用onTick .而是仅调度下一个tick(将调用onFinish()方法).

鉴于硬件时钟并不总是非常精确,后台可能还有其他进程延迟运行CountDownTimer的线程加上Android本身在调用CountDownTimer的消息处理程序时可能会产生一个小延迟,这很可能是倒计时结束前最后一次打勾的调用将至少延迟一毫秒,因此不会调用onTick().

对于我的应用程序,我只是通过使滴答间隔"稍微"更小(500毫秒)解决了这个问题

    myCountDownTimer = new CountDownTimer(countDownTime, intervalTime - 500) {
                                   ...
    }
Run Code Online (Sandbox Code Playgroud)

我可以保留我的代码.对于间隔时间长度至关重要的应用,此处发布的其他解决方案可能是最好的.

  • 是的,或者你可以说"countDownTime + 900"或类似的东西.倒数时间为10000,间隔为1000,如果每个滴答都能准确按时触发,则只能看到10个滴答.在这个例子中增加900毫秒不会给它足够的时间再多1个滴答,但是会给它足够的时间用于"最后"滴答,并且时钟的一些余量不是精确按计划进行的.(例如,当最后一个节拍从开始时的"10004"毫秒点击时,你会错过它但是如果你添加了一点填充,那就没问题) (2认同)
  • 请将@JamieB 的建议添加到您的答案中。这样,即使您的应用程序在抽动之间需要特定的时间,它也会起作用。我注意到我的设备有 6 毫秒的延迟。所以 `countDownTime+200` 应该在大多数情况下解决它,并使其几乎无法被用户识别 (2认同)

oca*_*nal 22

我不知道为什么最后一个滴答不起作用,但你可以用Runable创建自己的定时器,例如.

class MyCountDownTimer {
    private long millisInFuture;
    private long countDownInterval;
    public MyCountDownTimer(long pMillisInFuture, long pCountDownInterval) {
            this.millisInFuture = pMillisInFuture;
            this.countDownInterval = pCountDownInterval;
        }
    public void Start() 
    {
        final Handler handler = new Handler();
        Log.v("status", "starting");
        final Runnable counter = new Runnable(){

            public void run(){
                if(millisInFuture <= 0) {
                    Log.v("status", "done");
                } else {
                    long sec = millisInFuture/1000;
                    Log.v("status", Long.toString(sec) + " seconds remain");
                    millisInFuture -= countDownInterval;
                    handler.postDelayed(this, countDownInterval);
                }
            }
        };

        handler.postDelayed(counter, countDownInterval);
    }
}
Run Code Online (Sandbox Code Playgroud)

并开始它,

new MyCountDownTimer(10000, 2000).Start();
Run Code Online (Sandbox Code Playgroud)

编辑GOOFY的问题

你应该有一个变量来保持计数器状态(布尔值).然后你可以写一个像Start()的Stop()方法.

编辑2为GOOFY的问题

实际上停止计数器没有错误,但在停止(恢复)后再次启动时会出现错误.

我正在编写一个新的更新的完整代码,我刚刚尝试过,它正在运行.这是一个基本的计数器,在屏幕上显示启动和停止按钮的时间.

柜台班

public class MyCountDownTimer {
    private long millisInFuture;
    private long countDownInterval;
    private boolean status;
    public MyCountDownTimer(long pMillisInFuture, long pCountDownInterval) {
            this.millisInFuture = pMillisInFuture;
            this.countDownInterval = pCountDownInterval;
            status = false;
            Initialize();
    }

    public void Stop() {
        status = false;
    }

    public long getCurrentTime() {
        return millisInFuture;
    }

    public void Start() {
        status = true;
    }
    public void Initialize() 
    {
        final Handler handler = new Handler();
        Log.v("status", "starting");
        final Runnable counter = new Runnable(){

            public void run(){
                long sec = millisInFuture/1000;
                if(status) {
                    if(millisInFuture <= 0) {
                        Log.v("status", "done");
                    } else {
                        Log.v("status", Long.toString(sec) + " seconds remain");
                        millisInFuture -= countDownInterval;
                        handler.postDelayed(this, countDownInterval);
                    }
                } else {
                    Log.v("status", Long.toString(sec) + " seconds remain and timer has stopped!");
                    handler.postDelayed(this, countDownInterval);
                }
            }
        };

        handler.postDelayed(counter, countDownInterval);
    }
}
Run Code Online (Sandbox Code Playgroud)

活动课

public class CounterActivity extends Activity {
    /** Called when the activity is first created. */
    TextView timeText;
    Button startBut;
    Button stopBut;
    MyCountDownTimer mycounter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        timeText = (TextView) findViewById(R.id.time);
        startBut = (Button) findViewById(R.id.start);
        stopBut = (Button) findViewById(R.id.stop);
        mycounter = new MyCountDownTimer(20000, 1000);
        RefreshTimer();
    }

    public void StartTimer(View v) {
        Log.v("startbutton", "saymaya basladi");
        mycounter.Start();
    }

    public void StopTimer(View v) {
        Log.v("stopbutton", "durdu");
        mycounter.Stop();
    }

    public void RefreshTimer() 
    {
        final Handler handler = new Handler();
        final Runnable counter = new Runnable(){

            public void run(){
                timeText.setText(Long.toString(mycounter.getCurrentTime()));
                handler.postDelayed(this, 100);
            }
        };

        handler.postDelayed(counter, 100);
    }
}
Run Code Online (Sandbox Code Playgroud)

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">
    <TextView android:textAppearance="?android:attr/textAppearanceLarge" 
              android:text="TextView" android:layout_height="wrap_content" 
              android:layout_width="wrap_content" 
              android:id="@+id/time">
    </TextView>
    <Button android:text="Start" 
            android:id="@+id/start" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:onClick="StartTimer">
    </Button>
    <Button android:text="Stop" 
            android:id="@+id/stop" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:onClick="StopTimer">
    </Button>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

  • 绝对你是对的,我只想表明你可以用其他方法来做到这一点. (3认同)
  • @Goofy实际上当你停止它时它没有运行,但恢复它有一个问题.我在我的回答中添加了一个新的完整更新代码,也许它可以帮到你. (2认同)