how to set a countdown timer for a specific time - android

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

Related

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...

Timer Android counter

I'm trying to implement a Timer counter, but all that I see is CountDown and I don't want to count down time, I just want start from 00:00 and every second sum one second until 60:00, I've been trying to implement this :
1.-Android timer? How? - Answer
2.- Chronometer
And they didn't help me at all.. can you guide me how to start time from 00:00 and end in 60:00?
Answer here :
As Usama Zafar put his answer, I updated the code to control this CountDownTimer in case you'll need it...
I created
CountDownTimer cdtTimer;
Then the method I have to startTimer() that starts to count the time it's like this :
private void StartTimer(){
final long EndTime = 3600;
cdtTimer = new CountDownTimer(EndTime*1000, 1000) {
public void onTick(long millisUntilFinished) {
long secondUntilFinished = (long) (millisUntilFinished/1000);
long secondsPassed = (EndTime - secondUntilFinished);
long minutesPassed = (long) (secondsPassed/60);
secondsPassed = secondsPassed%60;
tvCounterTimer.setText(String.format("%02d", minutesPassed) + ":" + String.format("%02d", secondsPassed));
}
public void onFinish() {
tvCounterTimer.setText("done!");
}
}.start();
}
Then wherever I want to cancel this CountDownTimer I simply do
cdtTimer.cancel();
The simplest way to implement Timer is to obviously use CountDown as stated on Android's official guide:
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
But as per your requirement you don't want to count down rather you wish to count up until you reach 60min mark. How can we do it is indeed an interesting question. You can try implementing a Runnable and get system time in milliseconds on each tick using: System.currentTimeMillis().
It tends to get complex right? So why not play with the CountDown Timer and make it work to our needs? How can we do it? Observe the below code:
long StartTime = 0; //Starting from 00:00
long EndTime = 3600; //End at min:sec converted to seconds
new CountDownTimer(EndTime*1000, 1000) {
public void onTick(long millisUntilFinished) {
long secondUntilFinished = (long) (millisUntilFinished/1000);
long secondsPassed = (EndTime - secondUntilFinished);
long minutesPassed = (long) (secondsPassed/60);
secondsPassed = secondsPassed%60;
// So now at this point your time will be: minutesPassed:secondsPassed
mytextView.setText(String.format("%02d", minutesPassed) + ":" + String.format("%02d", secondsPassed));
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
The code is very easy to understand. If you still have any queries comment them and I will address them. Hope this is helpful.

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

Android CountDownTimer onTick method not called

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

how to use timer in minutes and seconds

I am using a Timer in my android app.
This is what i am using,
Timer t = new Timer();
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {
public void run()
{
//Called each time when 1000 milliseconds (1 second) (the period parameter)
runOnUiThread(new Runnable() {
public void run()
{
TextView tv = (TextView) findViewById(R.id.timer);
tv.setText(String.valueOf(time));
time += 1;
}
});
}
},
//Set how long before to start calling the TimerTask (in milliseconds)
0,
//Set the amount of time between each execution (in milliseconds)
1000);
In my code, there's only seconds as you can see But I want it in Minutes and seconds like 00:01.
So, how to do it. Please help me.
Thanks in Advance.
An approach to obtain the String you're looking for, would look like.-
int seconds = time % 60;
int minutes = time / 60;
String stringTime = String.format("%02d:%02d", minutes, seconds);
tv.setText(stringTime);
If you need to show the results only in your second Activity, I'd recommend passing time value into args bundle, and generate the String from the activity which will display it.

Categories

Resources