Pause and resume android countdown timer - android

I'm having android countdown timer which is starting automatically. I need to pause and resume it. I've seen other solutions but they didn't work. My code is:
class Timer extends CountDownTimer
{
public Timer(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
timerTimedOut = false;
}
#Override
public void onFinish() {
if(timerTimedOut){
doSTH();
} else {
doSTHElse();
}
this.start();
}
#Override
public void onTick(long millisUntilFinished)
{
timerShow.setText(Long.toString(millisUntilFinished / 1000));
}
public void stop(){
timerTimedOut = true;
this.cancel();
onFinish();
}
}
What should I do to pause and resume it?

I have been dealing with this and after trying many things, I found this solution. Here you can find a great alternative for the Android CountDownTimer class:
https://gist.github.com/bverc/1492672
You just have to create a new class named CountDownTimer2, and paste that code. Then, use it instead of the normal CountDownTimer class, for example:
CountDownTimer2 cdt = new CountDownTimer2(30000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
//Whatever you want to do onTick
}
#Override
public void onFinish() {
Log.i(TAG, "Timer finished");
}
};
cdt.start();
}
An then you just need to pass
cdt.pause();
or
cdt.resume();
Hope it helps.

Related

android countdown timer is finished

In my activity i want to check if countdowntimer is finished i'll stop the activity and pass to another activity?How can i do this?
I define countdowntimer like this
mcountdowntimer = new CountDownTimer(25000,1000) {
#Override
public void onTick(long millisUntilFinished) {
Log.v("Log_tag", "Tick of Progress" + i + millisUntilFinished);
i++;
mProgressBar.setProgress(i);
}
#Override
public void onFinish() {
i++;
mProgressBar.setProgress(i);
}
};
mcountdowntimer.start();
mProgressBar.setProgressTintList(ColorStateList.valueOf(Color.rgb(64,91,164)));
}
Just simply add that logic in your onFinish() method:
#Override
public void onFinish() {
//here add the code for starting a new activity
}

Android game countdown timer

I'm working on a game where I need a countdown timer.
I need to be able to pause the timer, resume the countdown and to add some time to current countdown state.
I've looked at CountdownTimer class and its methods, but it seems like it doesn't have the required features.
I need advice - which component is best for this case?
How to use it?
What are the possible problems?
Thread? AsyncTask? Timer?
Does anyone have experience with this?
I think Thread can be used, but its easier to implement your features using CountdownTimer wrapper class:
static class MyCountdownTimer {
long mCurrentMilisLeft;
long mInterval;
CountdownTimerWrapper mCountdownTimer;
class CountdownTimerWrapper extends CountDownTimer{
public CountdownTimerWrapper(long millisInFuture,long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
}
#Override
public void onTick(long millisUntilFinished) {
mCurrentMilisLeft = millisUntilFinished;
}
}
public MyCountdownTimer(long millisInFuture, long countDownInterval) {
set(millisInFuture,countDownInterval);
}
public void pause(){
mCountdownTimer.cancel();
}
public void resume(){
mCountdownTimer = new CountdownTimerWrapper(mCurrentMilisLeft, mInterval);
mCountdownTimer.start();
}
public void start(){
mCountdownTimer.start();
}
public void set(long millisInFuture, long countDownInterval){
mInterval = countDownInterval;
mCurrentMilisLeft = millisInFuture;
mCountdownTimer = new CountdownTimerWrapper(millisInFuture, countDownInterval);
}
}

CounDownTimer how to set it for counting 2 times?

I'm doing an app which will use CountDownTimer
I would like to use 1 countdowntimer for 2 count downs.
If the time finishes, then a new time starts immediately, and so on.
For now I have something like this:
cdt = new CounDownTimer(time,1000) {
public void onTick(…) {
//code
}
public void onFinish() {
//and here I'm thinking to add a new time, but it doesn't work
}
};
How to do that? Or maybe is there other easier option to solve that problem?
Thanks for help!
Somewhere in your class newMyCountdownTimer(time, interval).start();
private class MyCountdownTimer extends CountDownTimer
{
public MyCountdownTimer(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish()
{
if (somecondition is satisfied) // be carefull otherwise the countdown keeps running
{
// newTime and newInterval are variables in the outer class
new MyCountdownTimer(newTime, newInterval).start();
}
}
#Override
public void onTick(long millisUntilFinished)
{
// TODO Auto-generated method stub
}
}

How to add and remove time on CountDownTimer?

The question is: How can I add or remove time from the CountDownTimer during the count down?
For example: The user does something good: +5sec, the user does something wrong: -5sec.
Can someone help me with some code?
Maybe something like this
abstract class MyTimer {
public MyTimer(long deadline, long interval)
{
mDeadline = deadline;
mInterval = interval;
mTimer = new MyCountDownTimer(mDeadline, mInterval);
}
public synchronized void start() {
mTimer.start();
}
public abstract void onTick(long time);
public abstract void onFinish();
public synchronized void userDidRight()
{
mTimer.cancel();
mTimer = new MyCountDownTimer(mDeadline, mInterval += 5000);
mTimer.start();
}
public synchronized void userDidWrong()
{
mTimer.cancel();
mTimer = new MyCountDownTimer(mDeadline, mInterfval -= 5000);
mTimer.start();
}
private class MyCountDownTimer extends CountDownTimer() {
private abstract void onFinish() {
MyTimer.this.onFinish();
}
private abstract void onTick(long time) {
MyTimer.this.onTick(time);
}
}
private MyCountDownTimer mTimer;
}
You could restart the timer every time the user changes:
class Timer {
private long remainingTime;
private CoundDownTimer timer;
public void addTime(long addedTimeInMillis) {
createNewTimer(remainingTime + addedTimeInMills);
}
public void createNewTimer(long timeInMillis) {
if(timer != null) {
timer.cancel();
}
timer = new CountDownTimer(timeInMillis, 1000) {
#Override
public void onTick(final long millisUntilFinished) {
remainingTime = millisUntilFinished;
}
#Override
public void onFinish() {
// do something here
}
}.start();
}
}
private class startTimer extends CountDownTimer
{
public startTimer(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
}
#Override
public void onTick(long millisUntilFinished) {
currenttime=millisUntilFinished;
textview.setText("" + currenttime / 1000);
}
#Override
public void onFinish() {
textview.setText("done");
}
}
The above class is a simple derived class of CountDownTimer
CountDownTimer timer = new CountDownTimer(30000,1000);
timer.start();
long currentTime;
For example the above timer starts from 30 secs,decreases by one for each second.
To increase or decrease the timer dynamically you can cancel the old timer and initialize with your new time as shown below
timer.cancel();
timer = new CountDownTimer(currentTime+5000,1000);// +5000 to increase by 5 secs
timer.start();
you can make timer,currentTime variables as global then you can use the two different parts of code at different location.

TimerTask in Android? [duplicate]

I have got an application where I need to show counter from 3 to 1 then quickly switch to another activity. Will TimerTask will be suitable for doing this? Can anybody show me an example of exactly how to do it?
CountDownTimer Worked. Code for showing timer for 3 seconds is.
new CountDownTimer(4000, 1000) {
public void onTick(long millisUntilFinished) {
Animation myFadeOutAnimation = AnimationUtils.loadAnimation(countdown.this, R.anim.fadeout);
counter.startAnimation(myFadeOutAnimation);
counter.setText(Long.toString(millisUntilFinished / 1000));
}
public void onFinish() {
counter.setText("done!");
}
}.start();
I would better use a CountDownTimer.
If you want for example your counter to count 3 seconds:
//new Counter that counts 3000 ms with a tick each 1000 ms
CountDownTimer myCountDown = new CountDownTimer(3000, 1000) {
public void onTick(long millisUntilFinished) {
//update the UI with the new count
}
public void onFinish() {
//start the activity
}
};
//start the countDown
myCountDown.start();
Use CountDownTimer as shown below.
Step1: create CountDownTimer class
class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onFinish() {
dialog.dismiss();
// Use Intent to Navigate from this activity to another
}
#Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
}
}
Step2: create an object for that class
MyCount counter = new MyCount(2000, 1000); // set your seconds
counter.start();

Categories

Resources