How i can make it without bug - Java - java

I have a time counter bug in the game, how to fix it?
In my game, when there are 6 seconds left, the timer turns red and the animation is played, but sometimes the code freezes :(
It is this code that is bugging me, how to implement it differently:
if (mTimeLeftInMillis == 6000){
mTextViewCountDown.setTextColor(Color.RED);
mTextViewCountDown.startAnimation(zooming_second);
soundPlay(watch);
}
Here is the detailed code:
private void startTimer() {
mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
#Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
if (mTimeLeftInMillis == 6000){
mTextViewCountDown.setTextColor(Color.RED);
mTextViewCountDown.startAnimation(zooming_second);
soundPlay(watch);
}
int minutes = (int) (mTimeLeftInMillis / 1000) / 60;
int seconds = (int) (mTimeLeftInMillis / 1000) % 60;
String timeLeftFormatted = String.format(Locale.ENGLISH, "%02d:%02d", minutes, seconds);
mTextViewCountDown.setText(timeLeftFormatted);
mTimerRunning = true;
}
#Override
public void onFinish() {
mTimerRunning = false;
TimeOutDialog();
soundPlay(time_out_sound);
mediaPlayer1.stop();
tadam_false.stop();
tadam_true.stop();
watch.stop();
mTextViewCountDown.clearAnimation();
}
}.start();
}

Related

I want to make it so that when the timer ends, when there are still 5 seconds left, an action occurs

How to make this action happen at the end of the time when there are still 5 seconds left.
This is the code of the action that I want to do when there are 5 seconds left from the timer:
mTextViewCountDown.setTextColor(Color.RED);
zooming_second = AnimationUtils.loadAnimation(Level1.this, R.anim.watch_zoo);
mTextViewCountDown.startAnimation(zooming_second);
soundPlay(watch);
In this method:
//variable start
private static final long START_TIME_IN_MILLIS = 60000;
private TextView mTextViewCountDown;
private CountDownTimer mCountDownTimer;
private boolean mTimerRunning;
private long mTimeLeftInMillis = START_TIME_IN_MILLIS;
//variable end
private void startTimer() {
mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
#Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
int minutes = (int) (mTimeLeftInMillis / 1000) / 60;
int seconds = (int) (mTimeLeftInMillis / 1000) % 60;
String timeLeftFormatted = String.format(Locale.ENGLISH, "%02d:%02d", minutes, seconds);
mTextViewCountDown.setText(timeLeftFormatted);
mTimerRunning = true;
}
#Override
public void onFinish() {
mTimerRunning = false;
TimeOutDialog();
soundPlay(time_out_sound);
mediaPlayer1.stop();
tadam_false.stop();
tadam_true.stop();
}
}.start();
}
private void pauseTimer () {
mCountDownTimer.cancel();
mTimerRunning = false;
}
Inside onTick(long) method simply check how many seconds are left. And if it is five, trigger your action.
EDIT
My approach to hitting the right time would be something like this:
if (Math.abs(mTimeLeftInMillis - 5000) < 100) {
// start the animation
}
This would avoid issues with if (mTimeLeftInMillis == 5000) as it gives it some buffer. It's needed because it will not trigger at exactly 5000 milliseconds.
Thanks! I did it like this:
if (mTimeLeftInMillis <= 6000){
mTextViewCountDown.setTextColor(Color.RED);
zooming_second = AnimationUtils.loadAnimation(Level1.this,
R.anim.watch_zoo);
mTextViewCountDown.startAnimation(zooming_second);
soundPlay(watch);
}

Change text from a class out

I created a simple Java class. This should spend the time. With System.out.println it works too! But how can I inform a certain TextView that it should also change?
Countdown_Test.java
public class Countdown_test {
private static long START_TIME_IN_MILLIS;
private CountDownTimer mCountDownTimer;
private boolean mTimerRunning;
private long mTimeLeftInMillis;
//private TextView timer;
public Countdown_test(long start_time) {
START_TIME_IN_MILLIS = start_time;
mTimeLeftInMillis = START_TIME_IN_MILLIS;
//timer = findViewById(R.id.timer); dosen't work
}
public void startTimer() {
mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
#Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
updateCountDownText();
}
#Override
public void onFinish() {
mTimerRunning = false;
}
}.start();
mTimerRunning = true;
}
public void updateCountDownText() {
int minutes = (int) (mTimeLeftInMillis / 1000) / 60;
int seconds = (int) (mTimeLeftInMillis / 1000) % 60;
String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
System.out.println(timeLeftFormatted);
}
}
MainActivity.java
Countdown_test ct = new Countdown_test(600000);
button_start.setOnClickListener(v -> {
ct.startTimer();
});
ct.updateCountDownText();
You could write an interface, a TimeLeftListener with a method onTimeLeftChanged(String timeleft). Then your activity extends TimeLeftListener and implements the onTimeLeftChanged()-method.
This is where you update your views.
When you initiate your CountDownTest you pass the activity like this.
Countdown_test ct = new Countdown_test(600000, this);
And create al listener in you CountDownTest-class like this
myTimeLeftListener =(TimeleftListener)activity;
Then in your updateCountDownText()-Method you put
myTimeLeftListener.onTimeLeftChanged(timeleft)
You could also use ViewModel and LiveData.

counter.Cancel(); isn't executed in OnStop(); method

I have a CounterTimer which starts when an Api is successful. if a user presses the KeyEvent.KEYCODE_HOME CounterTimer must cancel but instead of cancelling it keeps running
This is how i start the counter
private void countDownStart(String event_date_time) {
String[] tokens = event_date_time.split(":");
int secondsToMs = Integer.parseInt(tokens[2]) * 1000;
int minutesToMs = Integer.parseInt(tokens[1]) * 60000;
int hoursToMs = Integer.parseInt(tokens[0]) * 3600000;
long total = secondsToMs + minutesToMs + hoursToMs;
timer = new CountDownTimer(total, 1000) {
public void onTick(long millisUntilFinished) {
int seconds = (int) (millisUntilFinished / 1000) % 60;
int minutes = (int) ((millisUntilFinished / (1000 * 60)) % 60);
int hours = (int) ((millisUntilFinished / (1000 * 60 * 60)) % 24);
CountDown.setVisibility(View.VISIBLE);
CountDown.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds));
}
public void onFinish() {
CountDown.setVisibility(View.VISIBLE);
CountDown.setText("00:00:00");
loader.showProgressBar();
startActivity(new Intent(mContext, Questions.class));
}
};
timer.start();
}
and this is what I used to cancel. I've tried with both onStop and KeyEvent.KEYCODE_HOME
#Override
protected void onStop() {
System.out.println("count: onStop Called");
loader.hideProgressBar();
if (timer != null) {
timer.cancel();
}
super.onStop();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
loader.hideProgressBar();
System.out.println("KEYCODE_HOME");
if (timer != null) {
timer.cancel();
}
return true;
}
return false;
}
What could be possibly wrong here?

Issues with countdown timer android studio

I'm very new to Java and and I'm trying to make an activity that calls an countdown timer from 5:00 minutes to 0. But, I'm having some issues. I tried every example code out there. Can you guys help me?
import androidx.appcompat.app.AppCompatActivity;
import java.util.Locale;
public class Main4Activity extends AppCompatActivity {
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
Fixed!
int minute;
long min;
TextView tv_timer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
tv_timer=findViewById(R.id.contador);
minute=Integer.parseInt("5");
min= minute*60*1000;
counter(min);
}
private void counter(long min) {
CountDownTimer timer = new CountDownTimer(min, 1000) {
public void onTick(long millisUntilFinished) {
int seconds = (int) (millisUntilFinished / 1000) % 60;
int minutes = (int) ((millisUntilFinished / (1000 * 60)) % 60);
tv_timer.setText(String.format("%d:%d", minutes, seconds));
}
public void onFinish() {
Toast.makeText(getApplicationContext(), "Your time has been completed",
Toast.LENGTH_LONG).show();
}
};
timer.start();
}
}

Countdown timer still continues when onPause is called

I'm making a simple app where I start a countdown timer e.g. 25 to 0 and when the timer expires it shows a template.But when my mobile is locked timer will be paused and when i unlocked it will resume .what can i do for continues timer ??
public class MyActivity extends Activity {
TextView tvCountDown;
MyCount counter;
long countDownInterval = 1000;
#Override
public void onCreate(Bundle savedInstanceState) {
setTheme(android.R.style.Theme_Light_NoTitleBar_Fullscreen);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auth);
tvCountDown = (TextView) findViewById(R.id.tvCountDown);
counter = new MyCount(6000, countDownInterval);
counter.start();
}
public class MyCount extends CountDownTimer
{
public MyCount(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
}
public void onTick (long millisUntilFinished)
{
Log.v("test","onTick Seconds: "+millisUntilFinished+" : "+(millisUntilFinished / 1000));
tvCountDown.setText ( formatTime(millisUntilFinished));
System.out.print("");
}
public void onFinish() {
tvCountDown.setText("done!");
}
public String formatTime(long millis)
{
String output = "00:00:00";
long seconds = millis / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
seconds = seconds % 60;
minutes = minutes % 60;
hours = hours % 60;
String secondsD = String.valueOf(seconds);
String minutesD = String.valueOf(minutes);
String hoursD = String.valueOf(hours);
if (seconds < 10)
secondsD = "0" + seconds;
if (minutes < 10)
minutesD = "0" + minutes;
if (hours < 10)
hoursD = "0" + hours;
output = hoursD + " : " + minutesD + " : " + secondsD;
return output;
}
}
Just call .cancel() method when your activity/fragment going to destroy.
If you want to pause/resume yout timer go for this answer;

Categories