Android count down timer explanation wanted - android

in the android reference thing i found a page about countdowntimers : CountDownTimers
it has this piece of code inside it:
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
I wanted to know what it is and if its an object or class and how i can use it as a timer for objects of a different class. can someone please explain how i would put this into my android project and use it. thankyou.

the reference you quoted explains it very clear.
CountDownTimer is a CLASS and it's used to perform a determinated task until it's done.
The onTick method is used to perform an action on every tick (in milliseconds) and
public void onTick(long millisUntilFinished) {
//It will perform this task every millisecond until countdown finishes.
}
The onFinish method performs a last task until the class end's its instance.
public void onFinish() {
//It will perform this task only once, when the countdown finishes.
}

From the docs
public abstract class
CountDownTimer
extends Object
should answer one question.
As far as how to use it, see this answer

Related

can not run countdowntimer at all

I have a problem with countdown timer.I try some of solutions and articles in this site but they never worked for me. so, please read my codes...
also I used
handler.postDelayed(new Runnable() {
before and it was not my solution but it just worked correctly.
Main question is:
I want to do something like below:
(button pressed)
do some codes1
delay1
do other codes2
delay2
go back to *do some codes1* again.
In short, this is my real code:
itimesec--;
setdelay();
irepeat--;
setrelax();
and this is in my functions:
public void setrelax(){
CountDownTimer yourCountDownTimer1 = new CountDownTimer(50000, 1000) {
public void onTick(long millisUntilFinished1) {
itotalsnozee--;
TextToSpeechFunction(" "+itotalsnozee);
}
public void onFinish() {
itotalsnozee=fitotalsnozee;
isrelax=false;
TextToSpeechFunction("do again");
}
}.start();
yourCountDownTimer1.cancel();
}
I tried to use a variable insted of 50000 but it was not useful anyway.
I tried to put setrelax funtion codes directly into oncreate but it never worked.
it just jumped to
}.start();
yourCountDownTimer1.cancel();
every times and go out.
I tried all the codes without any delay function and they runned correctly.
what is my wrong please...
You need to remember that your code won't be executed sequentially when using the CountDownTimer because it's working asynchronously (via Handler).
Let's dissect your code. Your following code here:
public void setrelax(){
// 1. Creating CountDownTimer
CountDownTimer yourCountDownTimer1 = new CountDownTimer(50000, 1000) {
public void onTick(long millisUntilFinished1) {
// 2. onTick called
...
}
public void onFinish() {
// 3. onFinish called
...
}
}.start();
// 4. CountDownTimer is cancelled.
yourCountDownTimer1.cancel();
}
will be running in the following sequences:
Creating CountDownTimer
CountDownTimer is cancelled.
onTick called 49 times repeatedly (50000/1000 = 50 - 1 ).
onFinish called
So, change your algorithm to something like this:
do some codes1
delay1
--> When delay finished, do other codes2. Then do delay2
You need to call the next code in the CountDownTimer.onFinish()
I looked at your code and I could not find any big mistakes but try this instead
Instead of
.start();
use
yourCountDownTimer1.start();
and remove yourCountTimer1.cancel();
like this :
public void setrelax(){
CountDownTimer yourCountDownTimer1 = new CountDownTimer(50000, 1000) {
public void onTick(long millisUntilFinished1) {
itotalsnozee--;
TextToSpeechFunction(" "+itotalsnozee);
}
public void onFinish() {
itotalsnozee=fitotalsnozee;
isrelax=false;
TextToSpeechFunction("do again");
}
};yourCountDownTimer1.start();
}
Hope it helps.
below is the code for running otp timer in our code.you can copy paste i have mentioned the comments please follow the same.
CountDownTimer countDownTimer; //define countDownTimer
countDownTimer.start(); // start countdown timer on some event like on click
String x="Your code will expire In";
String y="mins";
counttimer(); call the method counttimer which includes all the code of timer
public void counttimer(){
countDownTimer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
String text = String.format(Locale.getDefault(), x+" %02d : %02d "+y,
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) % 60,
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) % 60);
phonever_timer.setText(text); //set timer into textview object
}
public void onFinish() {
phonever_timer.setText("Otp Expired..!");
}
};

Make a global timer

I have 3 buttons.
If I click on a button, I have to disable all the buttons until the countdown timer expires.
I am trying to make a Global countdown timer.
Could you tell how to make it working?
I haven't tried this myself, but some quick searching found a CountDownTimer class in Android that looks like it might work for you.
The example code they give looks pretty straightforward:
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();

CountDownTimer skipping one second when displaying on TextView

I want to display a simple CountDownTimer in a TextView, but it seems to go from 30 down to 28 right away, as if there was a lag in between. I'm not sure how to go about on fixing this small bug. Here is my code:
This is in the click listener of the Button:
new CountDownTimer(30000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
coolDownTimer.setText("Cool Down for: " + String.valueOf(millisUntilFinished / 1000));
}
#Override
public void onFinish() {
animatedPic.setClickable(true);
// reregister the proximity sensor
sm.registerListener(sensorListener, proxSensor, SensorManager.SENSOR_DELAY_NORMAL);
coolDownTimer.setText("GO!");
}
}.start();
There are 2 issues:
The first issue is that the countdown timer doesn't execute until time has elapsed for the first tick, in this case, after 1000ms.
The second is that the elapsed time is only approximate. millisUntilFinished is not guaranteed to come in increments of the interval (you can see that if you don't divide by 1000, the first tick is slightly under 29000).
Another thing to keep in mind is that you're not guaranteed to receive a tick call. That is to say, if the device did not have enough time to complete a tick, it may skip it (generally only noticeable for faster intervals).
To solve issue 1, you can simply run the onTick code (or refactor it into its own method) and run it as you start the countdown timer.
For issue 2, you can simply round the number.
For example:
new CountDownTimer(30000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
performTick(millisUntilFinished);
}
#Override
public void onFinish()
{
animatedPic.setClickable(true);
// reregister the proximity sensor
sm.registerListener(sensorListener,proxSensor,SensorManager.SENSOR_DELAY_NORMAL);
coolDownTimer.setText("GO!");
}
}.start();
performTick(30000);
void performTick(long millisUntilFinished) {
coolDownTimer.setText("Cool Down for: " + String.valueOf(Math.round(millisUntilFinished * 0.001f)));
}
If you want to make sure the appropriate value is updated with minimal latency, you may want to consider reducing the interval.
It is skipping because CountDownTimer is not a precise timer. Try to print just the millisUntilFinished without dividing it you'll see some excess number that it wont be exactly 29000 when it hits its first tick.
you can refer on this thread for the solution.

Multiple CountDownTimers with a uiversal Cancel command

Im back to ask another question :)
In an app I am making, I need a selector to choose one of 4 different countdown timers, for example round1, round2, etc.
but i have 2 problems with the same source. Using the default countdowntimer implentation which doesnt involve naming it round1 but running as a straight countdowntimer, I can cancel it in onPause using
coutdowntimer.cancel();
but after naming it round1 I can no longer pause it and the round1.cancel(); comes up as undefined.
I have spent most of yesterday and throught to 3am trying to find a solution and i thought i had it by making my timers final but it still didnt work.
after thinking about it more i relaised once ive solved this problem, I'm going to have to implement an if statement in my onpause which i would rather not do in an effort to keep my code clean.
So is there a way to implement a universal cancel command to cancel the root countdowntimer function regardless of the actual name its running under.
for example
public void canceltimers(){
//if round1,2,3,4 is runnng, cancel the main countdowntimer thread altogether.
countdowntimer.Cancel();
}
any help is appreciated and here is my current timer code.
CountDownTimer round1 = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
textview4.setText("" + millisUntilFinished / 1000);
}
public void onFinish() {
endsequence();
}
};
CountDownTimer round2 = new CountDownTimer(20000, 1000) {
public void onTick(long millisUntilFinished) {
textview4.setText("" + millisUntilFinished / 1000);
}
public void onFinish() {
endsequence();
}
};
// round1.start();
ive not implemented my if statements for the level select yet but will be similar to this
if (level == 1){
round1.start();}
if (level == 2){
round2.start();}
So far i have tried setting it as final, with no joy.
I have tried
round1.cancel();
with no joy. as well every implementation of countdowntimer i can think of or find.
Now as a request, im not as amazing as making sense of code as most of you guys so if you could try and give an example of the implementation it would be appreciated, Thanks for looking and thanks for your time & help
phil
make a single timer. Add multiple timertasks to this timer. Define the COUNTDOWN_TIME.
Timer mTimer = new Timer();
mTimer.schedule(new TimerTask() {
#Override
public void run() {
//execute code when the COUNTDOWN_TIME has ran out.
}
}, COUNTDOWN_TIME);
}
//cancels all sceduled/running TimerTasks
mTimer.cancel();

Stopwatch that works on background

Are there any sample stopwatch project ( or tutorial ) that stopwatch timer will be shown in notificationbar and will keep working even if application closed ?
A simple countdown timer will work, and will persist through onPause() if you allow it to.
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
Refer to this source for more information. You can also update TextViews if you need to show the timer.
Edit for count up specifically, try Chronometer, which can be found at this link. You can also find a demo of this in the Samples APK.

Categories

Resources