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();
Related
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();
When Timer Finish it goes back to previous Activity for a Second then Apply onFinish
What is the reason and how to prevent that.
mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
#Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
updateCountDownText();
}
#Override
public void onFinish() {
playAgain();
}
}.start();
The Reseaon is at the start of every tick, before onTick() is called, the remaining time until the end of the countdown is calculated and If this time is smaller than the countdown time interval, onTick() would not not called anymore.
When onTick() called CountDownTimer consume some time for calculation. the easiest way to handle this is to add some extra timeLeftInMillis to your timer!
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.
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!
CountDownTimer.cancel() is not working in the below code:
myTimer = new CountDownTimer(10000, 1000) {
public void onFinish() {
}
#Override
public void onTick(long millisUntilFinished) {
if(null != myObject){
myTimer.cancel();
}
}
}.start();
In the above code I have started a CountDownTimer which check if the object is not null and cancels the Timer accordingly. The object is set by some listener at any point of time.
Please refer and suggest. Am I doing the right thing here?
Solution By Gautier Hayoun :
Just made a drop-in replacement for
CountDownTimer that you can cancel
from within onTick : Github link–
Gautier Hayoun Dec 12 '10 at 1:04
Solution By Gautier Hayoun :
Just made a drop-in replacement for CountDownTimer that you can cancel from within onTick : Github link– Gautier Hayoun Dec 12 '10 at 1:04
Instead of CountDownTimer use TimerTask
final static long INTERVAL=1000;
final static long TIMEOUT=10000;
TimerTask task=new TimerTask(){
#Override
public void run() {
elapsed+=INTERVAL;
if(elapsed>=TIMEOUT){
this.cancel();
displayText("finished");
return;
}
//if(some other conditions)
// this.cancel();
displayText("seconds elapsed: " + elapsed / 1000);
}
};
Timer timer = new Timer();
timer.scheduleAtFixedRate(task, INTERVAL, INTERVAL);
private void displayText(final String text){
this.runOnUiThread(new Runnable(){
#Override
public void run() {
mTextField.setText(text);
}
});
}