Вот то, к чему я стремлюсь. сделать:
Код: Выделить всё
1. Start a 3-second countdown timer.
2. When it finishes, start the main timer.
3. Once the main timer completes, start the rest timer.
4. Repeat the entire sequence for the desired number of rounds (skipping the rest period on the last round).
Я думал об использовании цикла while, чтобы получить ПОРЯДОК вызова/ожидания, но первый CountDownTimer так и не завершился, чтобы изменить логическое значение isThreeSecondsCountDownTimerOver.
Код: Выделить всё
package com.example.empty_tv_test;
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import android.os.CountDownTimer;
import android.widget.TextView;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private Boolean isThreeSecondsCountDownOver = true;
private Boolean isTimerRunning = true;
private Boolean isMainCounterOver = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
// Initialize the TextView
textView = findViewById(R.id.textViewTimer);
// Start CDT 1
//Start the CountDown
//Avoiding infite runs
boolean didThreeSecondsRun = false;
boolean didMainTimerRun = false;
boolean didRestTimerRun = false;
while (isTimerRunning) {
//Run for as many rounds as necessary /
//TO-DO Wrap this with FOR loops for Rounds
//First Check: Is 3 Seconds Countdown?
if (isThreeSecondsCountDownOver && !didThreeSecondsRun){
StartCountDownTimer(2000, 1);
//Turn 3 SEC off
didThreeSecondsRun = true;
}
if (!isThreeSecondsCountDownOver && !didMainTimerRun){
//3 Seconds Coundown is over, start main counter
StartCountDownTimer(7000, 2);
didMainTimerRun = true;
isTimerRunning = false;
}
if(isMainCounterOver && !didRestTimerRun){
//Finally the Rest Round is Fired
StartCountDownTimer(8000, 3);
didThreeSecondsRun = true;
isTimerRunning = false;
}
}
}//END of onCreate
//TimerType: 1. 3 SEC, 2. Main, 3. Rest
public void StartCountDownTimer(int time, int whichTimerType){
new CountDownTimer(time, 1000) {
@Override
public void onTick(long millisUntilFinished) {
// Used for formatting digits to be in 2 digits only
NumberFormat f = new DecimalFormat("00");
long hour = (millisUntilFinished / 3600000) % 24;
long min = (millisUntilFinished / 60000) % 60;
long sec = (millisUntilFinished / 1000) % 60;
textView.setText(f.format(hour) + ":" + f.format(min) + ":" + f.format(sec));
}
@Override
public void onFinish() {
// When the task is over it will print 00:00:00
textView.setText("ZZZZZZZ");
if (whichTimerType == 1){
isThreeSecondsCountDownOver = false;
}
if (whichTimerType == 2){
isMainCounterOver = true;
}
if (whichTimerType == 3){
isMainCounterOver = false;
}
}
}.start();
}
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... -and-start