Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...