How to store countdown timer value in a variable? - java
So, I have a countdown timer in my activity. Now, I have a button which opens another activity. When that button is clicked I need to store the current countdown timer value, and start a countdown timer in the second activity from that saved value. I was thinking to pass the time using the Intent but I don't know what value to pass. How can I do that? My timer:
public String formatTime(long millis) {
String output = "00:00";
long seconds = millis / 1000;
long minutes = seconds / 60;
seconds = seconds % 60;
minutes = minutes % 60;
String sec = String.valueOf(seconds);
String min = String.valueOf(minutes);
if (seconds < 10)
sec = "0" + seconds;
if (minutes < 10)
min= "0" + minutes;
output = min + " : " + sec;
return output;
}
And in onCreate method:
// New timer for 40 minutes, starts after initialization
new MyCount(2400000, 1000)
{
// Updates the text on your "scoreboard" every second
public void onTick(long millisUntilFinished)
{
vreme.setText("" + formatTime(millisUntilFinished));
}
public void onFinish()
{
}
}.start();
}
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onFinish() {
}
public void onTick(long millisUntilFinished) {
vreme.setText("" + millisUntilFinished / 1000);
}
It doesn't look like the information you're looking for is available from the CountDownTimer. If you don't need to be very precise with your time then you can just pull the string out of vreme.
Assuming you want to be more precise than that, I would suggest saving a timestamp when the timer starts, and then get the difference between that and the current time when you need to know how much time has elapsed. Check out uptimeMillis() or elapsedRealtime() depending on your needs.
OK, to answer myself. Here's what I did. In my first activity I did all the stuff in original post, plus I added this, in MyCount method, right below vreme.setText....:
currentTime = millisUntilFinished;
And previously I declared in my activity scope:
long currentTime;
After that, on my button onClickListener I did this:
Intent i = new Intent(Game1.this, Game2.class);
i.putExtra("timeCode", currentTime);
startActivity(i);
After that in my Game2 class's onCreate method (also declared long variable):
Bundle extras = getIntent().getExtras();
if(extras !=null) {
currentTime = extras.getLong("timeCode");
}
Then this (it's basically the same code from the Game1 class, only one change, my passed variable instead that 240000 value):
new MyCount(currentTime, 1000)
{
// Updates the text on your "scoreboard" every second
public void onTick(long millisUntilFinished)
{
vreme.setText("" + formatTime(millisUntilFinished));
}
public void onFinish()
{
}
}.start();
}
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onFinish() {
}
public void onTick(long millisUntilFinished) {
vreme.setText("" + millisUntilFinished / 1000);
}
And it works like a charm!
Related
CountDownTimer been decreses by 2, which should be just 1 sec
I'm creating a quiz game, and there is a countdown timer. But, the countdown decreases by two seconds, which should be just one second. My code is working okay sometimes, but when I install it again, it decreases again in 2 seconds. countDownTimer = new CountDownTimer(timer, 1200) { #Override public void onTick(long l) { timeText.setText(String.valueOf(timeValue)); timeValue = timeValue - 1; if (timeValue == -1){ FLAG = 3; playAudio.setAudioforEvent(FLAG); timerDialog.timerDialog(); } } #Override public void onFinish() { // timerDialog.timerDialog(); } }.start();
How to run timer in the background when Activity finishes and then continue again
How to save timer in sharedpreferences and then fetch again when activity restarts I have to set a timer of 5 mins and it should running in the background and when activity restart it should continue and it should not be restart again from 5 mins remaining new CountDownTimer(300000,1000){ #Override public void onTick(long millisUntilFinished) { timer.setText("Time Left: "+String.format("%d : %d min(s)", TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished), TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)))); } #Override public void onFinish() { timer.setVisibility(View.GONE); } }.start(); And what will be variable in sharedpreferences class? public static final long OrderTimeLeft = 300000;
Updated Keep timer running in background This result can be achieved pretty easily by a simple trick. You just need to store the startTime of your timer. long startTime; // Global variable startTime = System.currentTimeMillis(); // save the time in preference new CountDownTimer(300000,1000){ //....... } before resuming // retrieve the startTime from preference // now calculate the remaining time long remainingTime = 300000 - System.currentTimeMillis() - startTime // start your counter from here Old answer you need to store the remainingTime from timer. long remainingTime; // Global variable new CountDownTimer(300000,1000){ #Override public void onTick(long millisUntilFinished) { remainingTime = 300000 - millisUntilFinished; //....... } onPause() store this value in sharedPref and onResume() retrieve the value and initiate your timer with that value
repeating cutdown timer after break
Hi I would like to repeat this timer (code below) for example 3 times in such a way that the repeated time shows up on tv2. I would also like to repeatability to be after a 10 second break. I really don't know how to do this . CountDownTimer countDownTimer = new CountDownTimer(4000, 1000) { #Override public void onTick(long millisUntilFinished) { tv2.setText("Przygotuj siÄ™:\n " + (millisUntilFinished / 1000)); } #Override public void onFinish() { }.start();
Automatically activate Countdown when moving to activity
When the 2nd activity start, I want a countdown timer to activate automatically.
After your activity is loaded and the contents are set add this code below: new CountDownTimer(30000, 1000) { public void onTick(long millisUntilFinished) { mTextField.setText("seconds remaining: " + millisUntilFinished / 1000); } public void onFinish() { mTextField.setText("done!"); } }.start();
How to loop countdown timer multiple times
I am having a problem repeating countdown timer multiple times,in my case 12 times. I have made two countdown timers,one for roundtime and one for pause and works great.Problem is when the pause is finihed I want that roundtime countdown starts again automaticly until 12 rounds with pause are finished. I use for loop.Is there any better way? Here is code: when user clicks the button countdown starts button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { long roundtime= Long.parseLong(String.valueOf(msg1))*1000;//user has picked roundtime Counting timer = new Counting(roundtime, 100);//class Counting extends CountdownTimer for(int i=0;i<12;i++){ timer.start(); // It goes just ones! } } And countdown timer: class Counting extends CountDownTimer { public Counting(long roundtime, long countDownInterval) { super(roundtime, countDownInterval); } #Override public void onTick(long millisUntilFinished) { textView.setText("" + String.format("%02d:%02d", TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes( TimeUnit.MILLISECONDS.toHours(millisUntilFinished)), TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds( TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)))); } #Override public void onFinish() { // When roundtime is finished,start pause time! int seconds = msg1 % 60; int minutes = (msg1 / 60) % 60; textView.setText(String.format("%02d : %02d", minutes, seconds)); long pause= Long.parseLong(String.valueOf(msg2))*1000; //user has picked pause time Counting2 timer2 = new Counting2(pause, 100); timer2.start(); } } class Counting2 extends CountDownTimer { public Counting2(long pause, long countDownInterval) { super(pause, countDownInterval); } #Override public void onTick(long millisUntilFinished) { textView2.setText("" + String.format("%02d:%02d", TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes( TimeUnit.MILLISECONDS.toHours(millisUntilFinished)), TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds( TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)))); } #Override public void onFinish() { int seconds = msg2 % 60; int minutes = (msg2 / 60) % 60; textView2.setText(String.format("%02d : %02d", minutes, seconds)); } } }); }
First off, you want it to loop 12 times. This isn't the issue at hand, however, you'll thank me when it only loops 11 times. it should be for(int i=0;i<=12;i++){ timer.start(); // It goes just ones! } As I do not know the implementation of Counting2, it appears to be getting hung up on the start function there. I do not know if it's overridden or not. If you have a similar onFinish method, this is due to a recursive call back on the original Counting timer. Because of this, it loops with a waiting period in between (It doesn't get a stack overflow 'as quickly' as a result).
Try busy waiting for the previous timer to finish and then start it again, and it'll do so 12 times. for(int i=0;i<12;i++){ while(!previousTimerActive); timer.start(); previousTimerActive = true; } And in your Counting2's onFinish() set the indicator previousTimerActive = false;. Now you just need to see what way you want this boolean previousTimerActive to be 'global' (so it can be accessed from both your activity and your class Counting2). You can get it and set it in the SharedPreferences or use a third-party tool like EventBus to send it's value from one to the other.