I have a problem with countdown timer.I try some of solutions and articles in this site but they never worked for me. so, please read my codes...
also I used
handler.postDelayed(new Runnable() {
before and it was not my solution but it just worked correctly.
Main question is:
I want to do something like below:
(button pressed)
do some codes1
delay1
do other codes2
delay2
go back to *do some codes1* again.
In short, this is my real code:
itimesec--;
setdelay();
irepeat--;
setrelax();
and this is in my functions:
public void setrelax(){
CountDownTimer yourCountDownTimer1 = new CountDownTimer(50000, 1000) {
public void onTick(long millisUntilFinished1) {
itotalsnozee--;
TextToSpeechFunction(" "+itotalsnozee);
}
public void onFinish() {
itotalsnozee=fitotalsnozee;
isrelax=false;
TextToSpeechFunction("do again");
}
}.start();
yourCountDownTimer1.cancel();
}
I tried to use a variable insted of 50000 but it was not useful anyway.
I tried to put setrelax funtion codes directly into oncreate but it never worked. it just jumped to
}.start();
yourCountDownTimer1.cancel();
every times and go out.
I tried all the codes without any delay function and they runned correctly.
what is my wrong please...
I looked at your code and I could not find any big mistakes but try this instead
Instead of
use
and remove yourCountTimer1.cancel(); like this :
Hope it helps.
You need to remember that your code won't be executed sequentially when using the
CountDownTimer
because it's working asynchronously (via Handler).Let's dissect your code. Your following code here:
will be running in the following sequences:
So, change your algorithm to something like this:
You need to call the next code in the
CountDownTimer.onFinish()
below is the code for running otp timer in our code.you can copy paste i have mentioned the comments please follow the same.