Android Countdown timer with double speed - android

I would like to create a countdown timer that starts at 10 but only takes 5 seconds to count down to 0.
I have this code below from Google Source Code that counts down from 10:
new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
timerText.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
timerText.setText("done!");
}
}.start();

You should actually try doing it yourself first, but:
new CountDownTimer(5000, 500) {
public void onTick(long millisUntilFinished) {
timerText.setText("Half-seconds remaining: " + millisUntilFinished / 500);
}
public void onFinish() {
timerText.setText("done!");
}
}.start();
The CountDownTimer has two parameters in its constructor, one for the length of the timer as a whole (called millisInFuture, the first parameter), and one for how often the onTick function is called (which is countDownInterval). Both of these parameters are of the long variable type.
Please see CountDownTimer in the Android API.

Related

Android: Get passed time of a normal CountDownTimer?

Is there a way to save the exact passed time (either milliseconds, seconds or minutes is fine) of a CountDownTimer?
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
I tried increasing a variable in onTick(), but the problem is, my timer is cancel and restartable over a Button, and onTick() gets called everytime I start the timer, no matter if a second passed or not. So if I click the Button multiple times in 1 second, onClick() gets called every single click, rather than once per second.
You can use System.currentTimeMillis(), is this what you are after?
public class MyCountDownTimer extends CountDownTimer {
long timeStart;
public MyCountDownTimer(long x, long y) {
super(x, y);
timeStart = System.currentTimeMillis();
}
#Override
public void onTick(long millisUntilFinished) {
long timeLapsed = System.currentTimeMillis() - timeStart;
/// your code
}
#Override
public void onFinish() {
/// your code
}
}

How to stop the CountDownTimer?

where should I edit this code, that i can stop the CountDown ?
How to delete the old one before I start the next new CountDown ?
public void MyCounter1(){
new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
txt_timer.setText("Left time : " + millisUntilFinished / 1000);
}
public void onFinish() {
txt_timer.setText("done");
}
}.start();
}
here is code:
CountDownTimer timer= null;
public void MyCounter1(){
timer =new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
txt_timer.setText("Left time : " + millisUntilFinished / 1000);
}
public void onFinish() {
txt_timer.setText("done");
}
};
timer.start();
}
// To stop & start new timer check not null of timer instance first then cancel existing timer & start new one
if(timer != null){
timer.cancel();
MyCounter1();
}
also if you want to cancel first instance and start new one you can add above lines in onFinish method which will be get called when timer finish his time.
check this:
CountDownTimer timer= null;
public void MyCounter1(){
timer =new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
txt_timer.setText("Left time : " + millisUntilFinished / 1000);
}
public void onFinish() {
txt_timer.setText("done");
if(timer != null){
timer.cancel();
MyCounter1();
}
}
};
timer.start();
}
I did these 2 lines. I works at the time fine:
public void MyCounter1(){
CountDownTimer countDownTimer = new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
if(my_condition=1){ //1- just make my_condition<>1 to stop the counter
txt_timer.setText("Left time : " + millisUntilFinished / 1000); }
}
public void onFinish() {
countDownTimer.cacel(); // 2- delete old one
txt_timer.setText("done");
}
}.start();
}
private int time = 10; //example 10 min.
CountDownTimer(60000 * time, 1000)
#Override
public void onFinish() {
txt_timer.setText("done");
cancel();
time = 0;

CountDownTimer not accurate

I have an Android service running in the background.
I want to be notified after a specific period of time (22 seconds), so I wrote:
private CountDownTimer mCountDownTimer = new CountDownTimer(22*1000,22*1000) {
public void onTick(long millisUntilFinished) {}
public void onFinish() {
doSomething();
}
};
I run this, and get the notification after 40 seconds, and even 50 seconds. Am I doing something wrong? How can this be done?
Actually you are putting interval time as wll 22000, what this you are doing wrong. Second parameter is the interval.So , Do this :
CountDownTimer alertTimer = new CountDownTimer(22*1000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
// Do here what you want
}
#Override
public void onFinish() {
}
}.start();

How to Pause CountDownTimer in Android when onClick

I have a timer running for my IQ activity, and I want to Pause that timer when user click on Answer Button.
Bellow is my running timer and I have cancel timer when onlick but it's doesn't work for me:
timer = (TextView) findviewbyid(r.id.time)
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
timer.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
banana.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
CountDownTimer.cancel();
}
}
You can create a variable 'total' for total time and just set: total = millisUntilFinished in the onTick method. Check this answer: https://stackoverflow.com/a/6469166/2126403

Android countdown timer display milliseconds?

I want a timer to pop up when I click a button that will count down from 3 seconds. And it does that fine but i want it to also show the milliseconds so when I click the button the text would go from 3.0 to 0.1. How would I add the milliseconds to the text view?
new CountDownTimer(1000, 3000) {
public void onTick(long millisUntilFinished) {
textViewTimer.setText("" + millisUntilFinished / 1000);
}
public void onFinish() {
textViewTimer.setVisibility(View.INVISIBLE);
textViewLevelGained.setVisibility(View.INVISIBLE);
}
}.start();
This is what I have
Other SO questions suggest CountDownTimer doesn't do sub 1-second granularity well. Look into a different class, like TimerTask.
Otherwise, the following would work.
new CountDownTimer(3000, 1) {
public void onTick(long millisUntilFinished) {
textViewTimer.setText("" + millisUntilFinished / 1000
+ "." + millisUntilFinished % 1000);
}
public void onFinish() {
textViewTimer.setVisibility(View.INVISIBLE);
textViewLevelGained.setVisibility(View.INVISIBLE);
}
}.start();

Categories

Resources