android CountDownTimer-未调用最后一个onTick-使用哪种清洁解决方案? [英] android CountDownTimer - last onTick not called - what clean solution to use?

查看:207
本文介绍了android CountDownTimer-未调用最后一个onTick-使用哪种清洁解决方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

沮丧的帖子...。

我偶然发现了 CountDownTimer-上一个onTick未被调用的问题,很多人都在这里报道过。

I just stumbled into the "CountDownTimer - last onTick not called" problem many have reported here.

显示问题的简单演示

package com.example.gosh;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;

public class CountDownTimerSucksActivity extends Activity {

int iDontWantThis = 0; // choose 100 and it works yet ...

private static final String TAG = "CountDownTimerSucksActivity";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    new MyCountDownTimer(10000 + iDontWantThis , 1000).start();
}

class MyCountDownTimer extends CountDownTimer {

    long startSec;

    public MyCountDownTimer(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        // TODO Auto-generated constructor stub
        startSec = System.currentTimeMillis() ;
    }

    @Override
    public void onFinish() {
        // TODO Auto-generated method stub
        Log.e(TAG, " onFinish (" + getSeconds() + ")");
    }

    @Override
    public void onTick(long millisUntilFinished) {
        // TODO Auto-generated method stub
        Log.e(TAG, millisUntilFinished + " millisUntilFinished" + " (" + getSeconds() + ")");

    }

    protected long getSeconds() {
        return  (((System.currentTimeMillis() - startSec) / 1000) % 60);

    }

}

}

测试运行的logcat输出...

The logcat output from a test run ...

您可以看到上一次调用onTick发生在1963ms millisUntilFinished上,然后下一个调用在大约2秒钟后onFinished了。肯定是越野车的行为。我发现了很多与此相关的帖子,但还没有干净的解决方案。如果将iDontWantThis字段设置为100,则源代码中包含一个。

As you can see the last call onTick is happening with 1963ms millisUntilFinished, then the next call is onFinished nearly 2 seconds later. Surely a buggy behavior. I found many posts on this yet no clean solution yet. One I included in the source code, if you set the iDontWantThis field to 100 it works.

我不介意次要领域的解决方法,但这似乎是一种核心功能,我无法理解它尚未修复。

I dont mind workarounds in minor fields yet this seems such a core functionality that i cant fathom it wasnt fixed yet. What are you people doing to have a clean solution for this?

非常感谢

马丁

更新:

Sam对CountDownTimer的非常有用的修改,不会使此处

A very useful modification of the CountDownTimer by Sam which does not surpresses the last tick due to internal ms delay and also prevents the accumulation of ms delay with each tick over time can be found here

推荐答案

您遇到的行为实际上是在<$ c $中明确定义的c> CountdownTimer 代码; 看看在源头

The behavior you are experiencing is actually explicitly defined in the CountdownTimer code; have a look at the source.

handleMessage()内的通知,如果剩余时间少于在此时间间隔内,它不会显式调用 onTick(),只是延迟到完成为止。

Notice inside of handleMessage(), if the time remaining is less than the interval, it explicitly does not call onTick() and just delays until complete.

但是请注意消息来源说, CountdownTimer 只是 Handler 的一个非常薄的包装,这是Android框架的真正计时组件。作为解决方法,您可以轻松地从此源(少于150行)创建自己的计时器,并删除此限制以获取最终的滴答回调。

Notice, though, from the source that CountdownTimer is just a very thin wrapper on Handler, which is the real timing component of the Android framework. As a workaround, you could very easily create your own timer from this source (less than 150 lines) and remove this restriction to get your final tick callback.

这篇关于android CountDownTimer-未调用最后一个onTick-使用哪种清洁解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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