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);
}
}
Related
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.
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
}
}
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.
Is there a timer functionality or timer that is a subclass of View in android that can be use to keep track the playing time of the user?
If I must build one, is it good to build a Thread or Runnable class that have a loop that put itself to sleep for 1000ms (Thread.sleep(1000)) then update itself. e.g.
public class TimerThread extends Runnable{
private int time;
boolean run = true;
public void run(){
while(run){
//update the View here
Thread.sleep(1000);
time++;
}
}
}
This might be helpful.
http://developer.android.com/reference/android/widget/Chronometer.html
I think you might need to Google for an example never really used it myself but I've heard of it.
Here is some code I use in one of my games. Hope it helps
MyCount counter = new MyCount(30000,10); //set to 30 seconds
counter.start();
public class MyCount extends CountDownTimer{
long time=0;
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
clock.setText("You lose!"); //clock is a textfield in the game
restart(); //call you own restart method
}
#Override
public void onTick(long millisUntilFinished) {
DecimalFormat df=new DecimalFormat("#.##"); //import java.text.DecimalFormat;
clock.setText("Left: "+ df.format(millisUntilFinished/1000.0));
time=millisUntilFinished;
}
public long getTime(){
return time;
}
}
Take look at.
http://developer.android.com/reference/android/os/CountDownTimer.html
Just to provide more options, here is another approach: Updating the UI from a Timer
I would be very grateful if someone could explain how to create a simple timer in a game which starts off at 30 seconds and then goes back until 0. At 0 I would then stop the game and invoke a method which would allow the next player to take a turn.
Any advice would be appreciated. Thanks
Edit:
import android.os.CountDownTimer;
import android.widget.TextView;
public class MyCount extends CountDownTimer {
static TextView timeDisplay;
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onFinish() {
timeDisplay.setText("Done!");
}
public void onTick(long millisUntilFinished) {
timeDisplay.setText("Left: " + millisUntilFinished / 1000);
}
}
In a seperate class I have created a method:
public void setTimer() {
timeDisplay = new TextView(this);
this.setContentView(timeDisplay);
MyCount counter = new MyCount(30000, 1000);
counter.start();
}
Within an onClick method I have called the method above:
public void onClick(View v) {
setTimer();
}
I'd appreciate any advice on this.