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
Related
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.
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);
}
}
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
I'm a beginner in android. Now i'm working on a simple app. I want to create a timer in app. I want it to countdown from 10 to 0 (it will be visible to the user) and when it is 0, it should do smth. It should start counting down when onTouch event is called. I tried this way, but it doesn't work. Can anyone help please?
here's my code:
final MyCounter timer = new MyCounter(10000,1000);
public class MyCounter extends CountDownTimer{
public MyCounter(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
System.out.println("Timer Completed.");
time.setText("Timer Completed.");
}
#Override
public void onTick(long millisUntilFinished) {
time.setText((millisUntilFinished/1000)+"");
System.out.println("Timer : " + (millisUntilFinished/1000));
}
}
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
timer.start();
}
return false;
The timer code looks correct. Let me check some assumptions:
1) "time" is a TextView
2) You actually hook the onTouchEvent up to something.
Set a breakpoint in onTouchEvent and verify it's getting called. Also, take a look at the Log class and LogCat to verify the methods are getting called.
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.