Android CountDownTimer onTick method not called - android

I have this code in Android
private void startStageTwoTimer(long timeUntilStageTwo) {
timer = new CountDownTimer(timeUntilStageTwo, 1000) {
public void onFinish() {
timer.cancel();
}
#Override
public void onTick(long millisUntilFinished) {
Log.v("millisUntilFinished", millisUntilFinished + "");
Calendar calendar = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("HH:MM:ss", Locale.getDefault());
calendar.setTimeInMillis(millisUntilFinished);
textView.setText(formatter.format(calendar.getTime()));
}
}.start();
}
where "timeUntilStageTwo" is time until some hour in next day, so I want to make it on every second (in the onTick method) to refresh the textView and change its text.
The problem is this onTick method is called just few times, and then stops being called at all, why is that? The point is I want to make a timer in the activity, that counts untill the given hour

Keep in mind that timeUntilStageTwo is milliseconds and not seconds.
So in case you need the timer to run for one hour, timeUntilStageTwo should be 3.600.000

Related

How to show countdown from timestamp?

I have to show countdown in my app,
i getting two timestamp
1. startTime
2. endTime
please help to show countdown timer.
Thank you
You can use CountdownTimer like this. It uses milliseconds, for example this uses 1 second interval(1000 milliseconds) as it counts down.
int input = Integer.valueOf(String.valueOf(mCount_Entry.getText())) * 1000;
CountDownTimer countDownTimer = new CountDownTimer(input, 1000) {
#Override
public void onTick(long millisUntilFinished) {
mDisplay.setText("seconds remaining: " + millisUntilFinished / 1000);
}
#Override
public void onFinish() {
mDisplay.setText("Countdown is over, boom*****");
}
}.start();

Android countdown timer keeps iterating between previous and current value

Hi I'm working on an app with a countdown timer. When the button is clicked the timer starts based on the user inputs into the text field,when I change the value of the text field and a new value for the timer starts, it flashes and shows the previous countdown value and counts both the values down,flashing between the previous and the current countdown value. How do I get the timer to forget the value from before and only use the current timer value.
I tried using the cancel function however that didn't work, I think it's something in my tick function however I'm not sure what it is.
Here is my code:
CountDownTimer mcountDownTimer = new CountDownTimer(getmonTime(), 1000) { // adjust the milli seconds here
public void onTick(long millisUntilFinished) {
long hr1=TimeUnit.MILLISECONDS.toHours( millisUntilFinished);
long sub1=hr1*60;
long min1=TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished);
long sub2=min1*60;
timer.setText(""+String.format("%d hr,%d min, %d sec",
TimeUnit.MILLISECONDS.toHours( millisUntilFinished),
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)-sub1,
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished)-sub2,
-
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
public void onFinish() {
timer.setText("0");
}
};
mcountDownTimer.start();
}
};
You probably don't cancel the previous one.
Save the instance of the CountDownTimer mcountDownTimer (BTW rename it to mCountDownTimer)
And before setting a new one, cancel the old one using:
mCountDownTimer.cancel();
and only then create the new instance.
E.g:
CountDownTimer mCountDownTimer;
#Override
public void onCreate...
...
if (mCountDownTimer != null) mCountDownTimer.cancel();
mCountDownTimer = new CountDownTimer...

Android countdown timer to time in future

I have an activity that receives two values, one for hour and one for minute. A future time, eg: 21:30
How can I set a countdown timer to run from the current time until the future time that was received ?
here is my timer code, currently set to 30sec for testing
CountDownTextView = (TextView) findViewById(R.id.CountDownTextView);
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
CountDownTextView.setText("" + millisUntilFinished / 1000);
}
public void onFinish() {
CountDownTextView.setText("Unlocking!");
}
}.start();
Many thanks
Calendar targetTime = Calendar.getInstance();
targetTime.set(Calendar.HOUR_OF_DAY, hour);
targetTime.set(Calendar.MINUTE, minute);
new CountDownTimer(targetTime.getTimeInMillis()-System.currentTimeMillis(), 1000) {
// here comes your code
}
First take future timeing then take current timeing calculate difference between it in terms of millisecond then pass this millisecond value as a input to countdown timer class its very easy way to do it

how to set a countdown timer for a specific time

I have a class that is taking two time values and finding that a given time interval is present within the time range by using system's current time. Now I want to set a countdown timer which should calculate the time left for a particular time interval. Can anyone show me how to set a countdown timer for calculating the time left for 'exact time' in this code?
// start time
String string1 = date + " 20:30:00";
Date time1 = new SimpleDateFormat("MM-dd-yyHH:mm:ss").parse(string1);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(time1);
// end time
String string2 = date + " 06:30:00";
Date time2 = new SimpleDateFormat("MM-dd-yy HH:mm:ss").parse(string2);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(time2);
// exact time
String string3 = date + " 04:36:00";
Date F = new SimpleDateFormat("MM-dd-yy HH:mm:ss").parse(string3);
Calendar c3 = Calendar.getInstance();
c3.setTime(F);
You can use a CountDownTimer to do this. Here is a class for CountDownTimer you can implement.
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long millisInFuture, long interval) {
super(millisInFuture, interval);
}
#Override
public void onFinish() {
my_textview.setText("Time's up!");
}
#Override
public void onTick(long millisUntilFinished) {
my_textview.setText("" + millisUntilFinished / 1000); //Shows no. of seconds left
//Uses a textview to update status
}
}
You can use this class within your activity. Inside the activity you can implement this
CountDownTimer countDownTimer = new MyCountDownTimer(millisInFuture, interval);
//In this case
//CountDownTimer countDownTimer = new MyCountDownTimer(50000, 1000);
Here millisInFuture is the "time left for your particular time interval" in milliseconds when you start the timer.
Let's say time left is 50 sec in this case. Then this value will be 50000
interval is the time in milliseconds in which you want to provide update. (i.e if you use 1000 as interval, the timer will update you every 1 second)
In this case
onTick(long millisUntilFinished)
is called every 1 second which gives you time left for the countdown timer in milliseconds.i.e. millisUntilFinished parameter gives you 49000 (i.e. 49 seconds left) when called for the first time and so on.
When the timer has finished countdown, onfinish() is called.
You can show this information in any way. I've simply shown it in a TextView.
The only step left for you is to actually start the countdown timer.
countDownTimer.start();
There is a good tutorial on countdown timer here also. http://androidbite.blogspot.com/2012/11/android-count-down-timer-example.html

CountDownTimer Check every two seconds

I have one CountDownTimer and i want to do an action for every two wasted seconds.
countdowntimer = new CountDownTimer(10000, 1) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
}
}.start();
For example when it starts time remaining = 10 seconds
When time remaining = 8 seconds I want to do an action
When time remaining = 6 seconds I want to do an action
and so on......
Just check if it is divisible by 2.
#Override
public void onTick(long millisUntilFinished) {
long seconds = millisUntilFinished/1000;
if ((seconds % 2) == 0)
{ // even number--do some action }
}
This is assuming you are calling the onTick() every second like
countdowntimer = new CountDownTimer(10000, 1000) {
This is probably preferable for you
Setting it up to call every 2 seconds should also work and better but leaving the original answer in case someone needs to call onTick() more often
countdowntimer = new CountDownTimer(10000, 2000) {
then you could just do the action with every call to onTick()
CountDownTimer Docs

Categories

Resources