CountDownTimer Text dissapears if a different Activity opened - Timer continues - android app - java

I have a countdowntimer that starts when a button is pressed.
Once you press the button - the button dims, and text on the button appears and counts down to 0 (when onFinish is reached the button becomes illuminated again and the text reads 'done').
I have a settings menu that allows for the addition of more timers and other settings - if a user starts a timer and it is working fine - then opens the settings menu and saves settings, they come back to the timer screen.
What they see is that the button is grayed out as if the timer is still counting down (which it is), but the text is no longer there counting down and when the timer finishes the button just remains dim.
Is there anyway to get the text counting down to be persistent in that activity even if another Activity is opened up temporarily (like the settings menu) so it will always show the appropriate timer countdown text? I'm still very new to android programming so any examples are appreciated.
TimerCode looks something like this:
//Timer Countdown
#Override
public void onTick(long millisUntilFinished) {
button.setText((millisUntilFinished/1000)+"");
button.getBackground().setColorFilter(android.graphics.Color.GRAY, Mode.MULTIPLY);
button.setTextColor(Color.GREEN);
button.setTextSize(24);
//Timer Finishes
#Override
public void onFinish() {
System.out.println("DONE");
button.setTextSize(44);
button.setText("UP");
button.getBackground().setColorFilter(null);
Button Code looks like this:
//Right Button1
final CountDown rButton1Timer = new CountDown(300000,200,bRightButton1);
bRightButton1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
rButton1Timer.start();
}
});
Ultimately I just want the onTick and onFinish to be persistent through whatever the user does - so if he opens up the settings and changes something, when he comes back to the timers they are still counting down.
Any ideas?

onResume was the culprit. I have since implemented onResume with success.

Related

Activity runs background, although back button is pressed

There is timer in GameActivity. After time finishes, end of round activity runs.
When I press back button in GameActivity, previous Activity (MainActivity) runs. But in background, GameActivity is still running. After time finishes, I see end of round activity screen although I am in MainActivity.
That means game activity runs in background. How can I stop GameActivity when I press back button?
I tried Finish() method.
#Override
public void onBackPressed() {
super.onBackPressed();
Intent i = new Intent(GameActivity.this,GameSettingsActivity.class);
startActivity(i);
finish();
}
You should stop your timer in the GameActivity's onPause() method. This will ensure your timer is stopped when hitting the back button but also when your app goes in background.
#Override
protected void onPause() {
super.onPause();
//stop your timer here...
}

How do I disable a button with a timer and keep it disabled on restart?

So my goal is to click a button, disable it and start a timer, once the timer is up enable the button. Simple right? You would do something like this.
button1.onClick {
button1.setEnabled(false);
new CountDownTimer(60000, 1000) { //Set Timer for 5 seconds
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
button1.setEnabled(true);
}
}.start()
}
However.. If the user closes the app while the timer is running the button will be enabled again, restarting the timer. so instead of having to wait for 60 seconds the user can just close the app and open it within 10 seconds.
So my question is, how do I disable the button for 60 seconds and keep it disabled even if the user closes and opens the app until 60 seconds has passed?
You have to persist that information within a data store that keeps it even when the app is switched off.
One way to do that would be to use https://developer.android.com/training/data-storage/shared-preferences
You have to get a timestamp when starting the timer, or compute the "end time" for the timer. You then save that information, and whenever the app starts up, you first check if your preferences contain such a time stamp. And if so, you check whether you are still in that "timed" window.
Thr key things to remember: you have to remove the persisted information when the timer is up, and: if somebody changes the system clock in the mean time, you got a problem, too. Dealing with that is possible, but requires more effort.
Try this with Handler.
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.setEnabled(false);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// This method will be executed once the timer is over
btn.setEnabled(true);
Log.d(TAG,"resend1");
}
},10000);// set time as per your requirement
}
});

How to Kill an activity on back button pressed

One of my activity displays a down count timer by the help of CountDownTImer() method. The onFinish() method of the Down count timer is displaying a toast to tell the user that the time has elapsed. When it reaches 00:00. The issue is when I press the back button before the time elapses I still get the toast message even if the activity is not visible.
So, I try to kill the activity on the back button pressed by overriding the onBackPressed() as follows, but still, the toast is displaying even if the activity is invisible.
public void onBackPressed(){
super.onBackPressed();
this.finish();
}
Why you don't cancel CountDownTimer() :
public void onBackPressed(){
yourCountDownTimer.cancel();
super.onBackPressed();
}

How do I delay a button performing an action by a certain timeframe, if the button wasn't pressed again in that timeframe?

Sorry for the terrible title, I am bad at describing these things.
I am building a metronome and have a (-) UI button that decreases the tempo by 1, and a (+) UI button that increases the tempo by 1.
My problem currently is that whenever I press either buttons, the metronome restarts itself since there's a new tempo, and plays immediately. So if you press the (-) button 10 times in a row, each time you press it you hear the initial metronome "beep".
I would like my app to do the following:
When the user clicks either (-) or (+) buttons, wait for 200 milliseconds
IF the user didn't click the buttons again in that timeframe, play the metronome
If the user DID click the button again, don't play the metronome, repeat the process: wait 200 milliseconds, if no click was made play the metronome, etc
The end result would be that if I'm at 100 bpm and I repeatedly press the (+) button 20 times until I am at 120 bpm, the metronome wouldn't start playing until I am done tapping.
How do I go about implementing this? Thank you!
Declare and instantiate the below in your activity:
private Handler timeoutHandler = new Handler();
private Runnable delayStartThread = new Runnable() {
public void run() {
startMetronome();
}
};
Then insert the below code block in your onClickListener for both + and - buttons:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
timeoutHandler.removeCallbacks(delayStartThread);
tempoOfMetronome++; //tempoOfMetronome--; for decrease button
stopMetronome();
timeoutHandler.postDelayed(delayStartThread, 200);
}
});
For more details on how the code works, refer the below links for examples (I used these examples to formulate the answer):
Android: clicking TWICE the back button to exit activity - How to use handler.postDelayed()
How to cancel handler.postDelayed? - How to cancel handler.postDelayed()
You should also look at the Android documentation for those methods.
If you want a delay between the action and the effect, there are several ways you can achieve it. This is one.
private boolean pressedAction = false;
#override
public void onClick(View v) {
if (pressedAction) return;
pressedAction = true;
new Thread(new Runnable(
#override
public void run() {
try {
Thread.sleep(200); // 200 miliseconds
} catch (Exception e) {}
// Update views or do work (program logic)
pressedAction = false;
}
}
}
Then, the metronome logic is your bussiness.

code to turn on transition setting on phone within app

I have this app where when it navigates to another activity it does a transition animation. Now I have already got that working properly but, to get it to work i have to turn the animation setting on my phone on. I have been searching on Google and I can"t seem to find solution to my problem. So my problem is this,is it possible to turn this setting on from within the app, with a code, instead of having to manually do it?
Here is the code but as i have said above this works perfectly and I'm using a Telstra next G Huawei phone, just want to no if i can code it so i can turn the animation setting on if i decide to put it on a different phone.
//Button to restart from beginning:
//Declaring the button for OnClickListener and EditText to send data:
private void Restart() {
Button Next = (Button)findViewById(R.id.restart3);
Next.setOnClickListener(new View.OnClickListener() {
//The action the button takes when clicked:
#Override
public void onClick(View v) {
//Goes back to the start of app:
Intent i = new Intent(Height.this, Page_1.class);
//clears the stack of all Activities that were open before this one so that they don't stack up:
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
//The transition animation when going onto next page:
overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
// to end the current activity:
finish();
}
});
}

Categories