T
T
Tswet2017-04-03 10:52:30
Java
Tswet, 2017-04-03 10:52:30

Waiting for the end of processing CountDownTimer before starting the next one in the loop?

Good afternoon.
I'm trying to run several CountDownTimers in a loop. But they start without waiting for the previous one to finish. and since they write to one text widget, the data is visible only from the last one launched. Running through onFinish is not an option, since the loop is nested in a loop.
data about the number of loop passes comes from another activity through the intent. The data exactly come - checked prints in the console. Code example:
package com.gmail.mtswetkov.countdowntimer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.TextView;
/**
* Created by Mikhail on 03/23/2017.
*/
public class TimerActivity extends Activity {
private static final int MILLIS_PER_SECOND = 1000;
public TextView chronView;
public TextView chStatus;
public CountDownTimer timer;
public int timerAll;
public int bigRest;
public int countEdu;
public int educ;
public int rest;
//public boolean isFinish = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
chronView = (TextView) findViewById(R.id.chronView);
chStatus = (TextView) findViewById(R.id.chronText);
//get data from Activity Main
Intent i = getIntent();
timerAll = i.getIntExtra(MainActivity.TIMER_ALL, 10);
bigRest = i.getIntExtra(MainActivity.BIG_REST, 10);
countEdu = i.getIntExtra(MainActivity.COUNT_EDU, 10);
educ = i.getIntExtra(MainActivity.EDU, 10);
rest = i.getIntExtra(MainActivity.REST, 10);
try {
for (int z = 1; z <= timerAll; z++) {
for (int j = 1; j <= countEdu; j++) {
chStatus.setText("WORKOUT");
Thread thread1 = new Thread(new ShowTimer((long) educ*MILLIS_PER_SECOND));
thread1.start();
try {
thread1.join(educ*MILLIS_PER_SECOND);
} catch (InterruptedException e) {
};
System.out.println("work");
chStatus.setText("REST");
Thread thread2 = new Thread(new ShowTimer((long) rest*MILLIS_PER_SECOND));
thread2.start();
try {
thread2.join(rest*MILLIS_PER_SECOND);
} catch (InterruptedException e) {
};
}
chStatus.setText("BIG REST");
if(z== timerAll){
chStatus.setText("DONE");
}
Thread thread3 = new Thread(new ShowTimer((long) bigRest*MILLIS_PER_SECOND));
thread3.start();
try {
thread3.join(bigRest*MILLIS_PER_SECOND);
} catch (InterruptedException e) {
};
System.out.println(bigRest);
}
} catch (NumberFormatException e) {
// method ignores invalid (non-integer) input and waits
// for something it can use
}
}
private class ShowTimer implements Runnable {
public ShowTimer(long countdownMillis) {
timer = new CountDownTimer(countdownMillis, 1000) {
@Override
public void onTick(long millisUntilFinished) {
System.out.println("enter");
chronView.setText(""+ millisUntilFinished/MILLIS_PER_SECOND);
}
@Override
public void onFinish() {
System.out.println("Done");
timer.cancel();
}
public void stop() {
timer.cancel();
timer = null;
}
};
}
@Override
public void run() {
timer.start();
}
}
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question