Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

android - How come millisUntilFinished cannot detect exact CountDownTimer intervals?

Hey all, so there is a rather small but annoying problem with the CountDownTimer.. Currently I have an interval set to 1000. And I am trying to detect the amount of milliseconds left in my

onTick()

method. So that I can use Text To Speech when there are 20 seconds left, 10 seconds left, etc. Well if I use:

//Speak when 20 seconds left
if (millisUntilFinished == 20000) {
    tts.speak("You have 20 seconds left.", TextToSpeech.QUEUE_FLUSH, null);
}

The timer is not able to detect the 20000 milliseconds exactly.

So I have to resort to using:

//Speak when 20 seconds left
if (millisUntilFinished < 20999) {
    if (millisUntilFinished > 19999) {
       tts.speak("You have 20 seconds left.", TextToSpeech.QUEUE_FLUSH, null);
     } 
}

The problem with that is that sometimes the text to speech will come on twice during that interval. Is there any way to detect the milliseconds exactly so I dont have to use greater than or less than?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...