How come millisUntilFinished cannot detect exact CountDownTimer intervals?

主宰稳场 提交于 2019-11-28 14:01:39

Is there any way to detect the milliseconds exactly so I dont have to use greater than or less than?

Of course not. Android is not a RTOS. Timing will be approximate, simply due to the main application thread being possibly occupied with other duties at any given moment.

tobi.b

If you want more flexibility you can also try something like

int secondsLeft = 0; 

new CountDownTimer(30000, 100) {
    public void onTick(long ms) {
        if (Math.round((float)ms / 1000.0f) != secondsLeft)
        {  
            secondsLeft = Math.floor((float)ms / 1000.0f);
            if (secondsLeft == 20) {
                tts.speak("You have 20 seconds left.", TextToSpeech.QUEUE_FLUSH, null);
            }
        }
    }

    public void onFinish() {
    }
}.start();

from https://stackoverflow.com/a/6811744/1189831

You could use a trick. If you use an Chronometer widget instead of TexView, you could make something like that:

int time = 30;

Chronometer chrono = (Chronometer) findViewById(R.id.your_chronometer_id);

chrono.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {

    @Override
    public void onChronometerTick(Chronometer chronometer) {

        chronometer.setText(String.valueOf(_time));

        if(_time == 20) tts.speak("You have 20 seconds left.", TextToSpeech.QUEUE_FLUSH, null);

        if(_time == 0){

            //Time finished, make your stuff

            chronometer.stop();     

        }else{

            _time--;

        }

    }

});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!