There is no way to find the solution
Thanks to @JorgeSys, I was able to resolve a question with the Timer and fortunately I have been able to solve that part of my code
Now my question is the following:
I have a for loop for the series
I have a for loop for repeats per series
Inside this loop, I would like to have the duration of the exercises and...
The duration of the rests between repetitions
my code is more or less the following
//bucle series
for (int i = 0; i < numSeries; i++) {
//bucle repeticiones
for (int j = 0; j < numRep; j++) {
//instancio primer Timer y ejecuto
timeSerie = new Timer();
timeSerie.schedule(new TimerTask() {
public void run() {
//descontamos 1 a la variable que contiene la duración
midTimeSerie--;
//calculamos los segundos
long ssSerie = midTimeSerie % 60;
//si los segundos son igual a 0
if (ssSerie == 0) {
//la variable coge la duración del ejercicio
midTimeSerie = segundosEjer;
//cancelamos el Timer actual
timeSerie.cancel();
//borramos el contenido del TextView
txtRelojEjer.setText("");
}
//si los segundos entan entre 9 y 1
else if ((ssSerie < 10) && (ssSerie > 0)) {
//escribimos los segundos en el TextView
txtRelojEjer.setText("00:0" + ssSerie + "");
//reproducimos un sonido
toneG.startTone(ToneGenerator.TONE_DTMF_0, 80);
}
else {
//escribimos los segundos en el textView
txtRelojEjer.setText("00:" + ssSerie + "");
}
}
//iteramos cada segundo
},1000);
midTimeDescanso = segundosDescEjer;
timeDescanso = new Timer();
timeDescanso.schedule(new TimerTask() {
public void run() {
midTimeDescanso--;
long ssDescanso = midTimeDescanso % 60;
if (ssDescanso == 0) {
midTimeDescanso = segundosDescEjer;
timeDescanso.cancel();
txtRelojDesc.setText("");
}
else if ((ssDescanso < 4) && (ssDescanso > 0)) {
txtRelojDesc.setText("00:0" + ssDescanso + "");
toneG.startTone(ToneGenerator.TONE_DTMF_0, 80);
}
else {
txtRelojDesc.setText("00:" + ssDescanso + "");
}
}
},1000);
}
}
But instead of executing the code of the first Timer, and carrying out the programmed process to then move on to the second Timer once the first one has finished, it executes the two Timers at the same time.
Could someone tell me how to make a complete Timer corresponding to the exercise, and once finished, move on to the second Timer corresponding to the rest?
Once the two Timers have finished, it would return to the For loop to continue with the number of repetitions.
At the end of the repetitions and its rests, it would go from the second loop to the first, which would go back for another loop as long as there are still sets.
edited
I am now with the following method, in it, more or less I have what I want except for several errors that I cannot solve
1 - I don't understand why in the first timer, when it reaches 9 it stops
2 - I don't understand why in the second timer, when it reaches 50 it stops
Despite having the timers in two for loops with multiple iterations, only one is performed.
Nor can I get inside the CountDownTimer when trying to debug the code, because it skips all the code that is inside the entire timer.
Can someone clarify my doubts?
I import the array data from another activity:
serie[0] = txtNombreSerie.setText("Regletas");
serie[1] = spiNumeroSeries.setSelection(3);
serie[2] = spiDescansoMSeries.setSelection(3);
serie[3] = spiDescansoSSeries.setSelection(0);
serie[4] = spiNumeroRepeticiones.setSelection(5);
serie[5] = spiTiempoMRepeticiones.setSelection(0);
serie[6] = spiTiempoSRepeticiones.setSelection(10);
serie[7] = spiDescansoMRepeticiones.setSelection(0);
serie[8] = spiDescansoSRepeticiones.setSelection(50);
method with the two loops and the two timer
private void metodoIniciarSerie(String[] serie) {
String nombreSerie = serie[0];
int numSeries = Integer.parseInt(serie[1]);
int numMSeries = Integer.parseInt(serie[2]);
int numSSeries = Integer.parseInt(serie[3]);
int numRep = Integer.parseInt(serie[4]);
int tiempoMRep = Integer.parseInt(serie[5]);
int tiempoSRep = Integer.parseInt(serie[6]);
int numMRep = Integer.parseInt(serie[7]);
int numSRep = Integer.parseInt(serie[8]);
int segundosEjer = ((tiempoMRep * 60) + tiempoSRep);
int segundosDescEjer = ((numMRep * 60) + numSRep);
int segundosDescSerie = ((numMSeries * 60) + numSSeries);
int segundosDesSeries = segundosDescSerie - segundosDescEjer;
contador = segundosEjer;
long tiempoEntreTicks = 1000; //un segundo
long duracionTotal = segundosEjer * 1000; //milisegundos
long duracionTotalD = segundosDescEjer * 1000; //milisegundos
for (int i = 0; i < numSeries; i++) {
for (int j = 0; j < numRep; j++) {
CountDownTimer ejercicio = new CountDownTimer(duracionTotal, tiempoEntreTicks) {
public void onTick(long milisegHastaFin) {
long segundosPasados = (duracionTotal + milisegHastaFin) / 1000;
int m = (int) segundosPasados / 60;
int s = (int) segundosPasados % 60;
txtDetalleEjer.setText("Trabaja Ejercicio");
txtDetalleDesc.setText("");
txtRelojEjer.setText(String.format("%02d:%02d", m, s));
if ((m == 0) && (s < 11) && (s > 0)) {
toneG.startTone(ToneGenerator.TONE_DTMF_0, 80);
}
else if ((m == 0) && (s == 0)) {
toneG.startTone(ToneGenerator.TONE_DTMF_0, 800);
}
}
public void onFinish() {
txtRelojEjer.setText("00:00");
CountDownTimer start = new CountDownTimer(duracionTotalD, tiempoEntreTicks) {
public void onTick(long milisegHastaFinD) {
long segundosPasadosD = (duracionTotalD + milisegHastaFinD) / 1000;
int md = (int) segundosPasadosD / 60;
int sd = (int) segundosPasadosD % 60;
txtDetalleDesc.setText("Descansamos");
txtDetalleEjer.setText("");
txtRelojDesc.setText(String.format("%02d:%02d", md, sd));
if ((md == 0) && (sd == 10)) {
toneG.startTone(ToneGenerator.TONE_DTMF_0, 80);
}
else if ((md == 0) && (sd < 4) && (sd > 0)) {
toneG.startTone(ToneGenerator.TONE_DTMF_0, 80);
}
else if ((md == 0) && (sd == 0)) {
toneG.startTone(ToneGenerator.TONE_DTMF_0, 800);
}
}
public void onFinish() {
txtRelojDesc.setText("00:00");
}
}.start();
}
}.start();
}
}
}
Thank you
Well, nothing, after a lot of research, testing, investigating, correcting, deleting, asking and experimenting, I have come up with a solution that may not be the perfect one but that works perfectly for me
I resort to recursion and eliminate the for loops, since the timers are loops themselves
I take advantage of the on_finish() method to call itself, this way the new countdown only starts when the previous one finishes