Multiple CountDown Timer running one after another - android

this is my first question here.
I need to implement in my app six countdown timers running one after another. When first finishes, next one starts and so on. Init time of every single run depends on user input. The problem is that I need to put different code in onTick() and onFinish() methods for every single countdown timer running and, well, I'm not sure how to start a next counter after one is finished. I was thinking about calling next counter in onFinish() method of current one but I can't figure out how to do this with 6 counters.
This is my Countdown timer class:
public class Counter extends CountDownTimer
{
public Counter(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
}
public void onTick(long millisUntilFinished)
{
//this code is the same for every counter
timer_view.setText(formatTime(millisUntilFinished));
//this code depends on specific counter running
output_view1.setText("My output here");
output_view2.setText("My output here");
output_view3.setText("My output here");
}
public void onFinish()
{
playSound(sound_id_1);
runMyMethod(user_input_1);
timerHasStarted = false;
}
}
I'm starting my counter in the same activity:
if(!timerHasStarted)
{
counter = new Counter(user_input1, 1000);
counter.start();
}

You probably need to break out the start timer functionallity and call it in onFinish().
public void startTimer(int counterId){
Counter counter = null;
switch(counterId){
case 0:
counter = new CounterOne(counterId,user_input1,1000);
break;
/* Counter 1-5 goes here*/
default:
break;
}
if(counter !=null ){
counter.start();
}
}
then start your next timer in onFinsh()
public abstract class Counter extends CountDownTimer
{
private int counterId;
public Counter(int counterId /*counter id start with 0*/,long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
this.counterId = counterId;
}
public abstract void onTick(long millisUntilFinished);
public void onFinish()
{
playSound(sound_id_1);
runMyMethod(user_input_1);
startTimer(this.counterId++);
}
}
public class CounterOne extends Counter{
public void onTick(long millisUntilFinished)
{
//counter 1 logic
}
}
/* Subclass others. eg. CounterTwo etc. */

You never set
timerHasStarted
to true. It's always false, so...yeah, one timer after another. Set it to true before calling counter.start() and it should work.

Related

Android - CountDownTimer will not use a variable as the first parameter

I'm trying to pass the countdowntimer a variable and use that variable as the amount of milliseconds to count down. If I simply enter the value the countdown works properly, but if I pass it a long variable it just runs the onFinish function.
Here's the actual code:
public CountDownTimer countDown = new CountDownTimer(respawnTime, 1000) {
#Override
public void onTick(long millisUntilFinished) {
timer =(Integer)(int) millisUntilFinished / 1000;
if(timer < 31)
timerText.setTextColor(Color.parseColor("#FF0000"));
timerText.setText(timer.toString());
}
#Override
public void onFinish() {
timerText.setTextColor(Color.parseColor("#00FF00"));
timerText.setText("UP");
}
};
At this point I have respawnTime set to equal 360000 hoping for a 360 second countdown, but like I said it just immediately runs the onFinish. Simply changing the first parameter to a literal instead of a variable fixes everything but I need to use a variable here. Thanks in advance for the help!
Change
#Override
public void onTick(long millisUntilFinished) {
to
#Override
public void onTick(long respawnTime) {
use the variable you are sending in the constructor in onTick().
Edit
Here is one of mine
private class MyCountDown extends CountDownTimer
{
long duration, interval;
public MyCountDown(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
duration = millisInFuture;
interval = countDownInterval;
start();
}
#Override
public void onFinish() {
secs = 10;
Intent intent = new Intent(CalibrationTimeoutScreen.this, CalibrationTakeTempScreen.class);
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(intent);
CalibrationTimeoutScreen.this.finish();
}
#Override
public void onTick(long duration) {
cd.setText(String.valueOf(secs));
secs = secs - 1;
}
}
You did not run start(). Simply add .start() after the last '}' or add a new line calling countDown.start().

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 timer to app

I am a new android developer. Recently, launched a word puzzle app - Wozzle
I want to add timer to the game so that a player gets a limited time to make a move. How to do that ? Also, the timer should be displayed to the player.
Try this,
public class CountDownTest extends Activity {
TextView tv; // textview to display the countdown
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
this.setContentView(tv);
// 5000 is the starting number (in milliseconds)
// 1000 is the number to count down each time (in milliseconds)
MyCount counter = new MyCount(5000, 1000);
counter.start();
}
//countdowntimer is an abstract class, so extend it and fill in methods
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
tv.setText("done!");
}
#Override
public void onTick(long millisUntilFinished) {
tv.setText("Left: " + millisUntilFinished/1000);
}
}
}
Have a look at these links
A Stitch in Time
On screen timer in Android application?
stop watch logic
Android Game Programming: The Game Loop

How to extend CountDownTimer to add pause and resume method?

I trying to extend ConuntDownTimer to add the methods pause and resume in this way:
public class CountDown extends CountDownTimer {
private long resume;
private long millisInFuture;
private long countDownInterval;
public CountDown(long millisInFuture, long countDownInterval) {
super(millisInFuture,countDownInterval);
resume = millisInFuture;
this.millisInFuture = millisInFuture;
this.countDownInterval = countDownInterval;
}
public void play() {
// start
if( millisInFuture == resume ) {
super.start();
// restart
} else {
CountDown cd = new CountDown(resume, countDownInterval);
cd.play();
}
}
#Override
public void onTick(long millisUntilFinished) {
resume = millisUntilFinished;
// other code
}
}
The problem is "restart" in the play method because, in this way, I created another istance of CountDown that display the wrong seconds because there are almost two event "onTick". Could I solve this problem? (I hope my English was understandable)
I don't see any way to do what you want by extending the class. You'll be best served writing your own. Here's the source for it.

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