Android countdown timer required

in the android help article i found a page about countdowntimers: CountDownTimers

he 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 it is an object or class and how I can use it as a timer for objects of another class. can someone please explain how i would put this in my android project and use it. many thanks.

+4
source share
3 answers

Common constructors:

CountDownTimer(long millisInFuture, long countDownInterval)

Common methods:

final void  cancel()
    Cancel the countdown.

abstract void   onFinish()
    Callback fired when the time is up.

abstract void   onTick(long millisUntilFinished)
    Callback fired on regular interval.

synchronized final CountDownTimer   start()
    Start the countdown.

Common constructors:

public CountDownTimer (long millisInFuture, long countDownInterval)
Parameters
1. millisInFuture   The number of millis in the future from the call to start() until the countdown is done and onFinish() is called.
2. countDownInterval    The interval along the way to receive onTick(long) callbacks.

Common methods:

public final void cancel ()
    Cancel the countdown.

public abstract void onFinish ()
    Callback fired when the time is up.

public abstract void onTick (long millisUntilFinished)
    Callback fired on regular interval.
        Parameters: millisUntilFinished     The amount of time until finished.

public final synchronized CountDownTimer start ()
    Start the countdown.

Links to textbooks:

reference 1

reference 2

reference 3

Edit:

new CountDownTimer(30000 /*For how long should timer run*/, 1000 /*time interval after which `onTick()` should be called*/) {

 public void onTick(long millisUntilFinished) {
     Log.i("Countdown Timer: ","seconds remaining: " + millisUntilFinished / 1000);
 }

 public void onFinish() {
     //Done timer time out.
     myTextView.setText("Time Out.!");
 }
}.start();
+6
source

the link you quoted explains this very clearly.

CountDownTimer - CLASS, .

onTick ( )

public void onTick(long millisUntilFinished) {
     //It will perform this task every millisecond until countdown finishes.
 }

onFinish , .

 public void onFinish() {
     //It will perform this task only once, when the countdown finishes.
 } 
0

From the documents

public abstract class

CountDownTimer

extends Object

must answer one question.

Regarding use, see this answer

0
source

All Articles