SoFunction
Updated on 2025-04-07

Summary of countdown methods for Android implementation

There is often a countdown function in Android development, and the following will summarize common centralized implementation methods.

1. Directly use Handler's message mechanism to implement it

The files in the xml layout are as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal" >

  <Button
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="clickButton"
    android:text="Start timing" />


</LinearLayout>

The java code is as follows:

import ;
import ;
import ;
import ;
import ;
import ;

public class FirstActivity extends Activity{

  private Button button;
  private int count = 60;
  private int COUNT_TIME = 0;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.first_activity);
    button = (Button) findViewById();

  }

  private Handler handler = new Handler(){

    @Override
    public void handleMessage(Message msg) {
      if(count <= 0){
        count = 60;
        ("Re-time");
        (true);
        return;
      }
      count--;
      (""+count);
      sendEmptyMessageDelayed(COUNT_TIME,1000);
    }
  };

  public void clickButton(View view){
    (COUNT_TIME);
    (false);
  }
}

2. Use Timer and TimerTask, and combine handler to achieve countdown

import ;
import ;

import ;
import ;
import ;
import ;
import ;
import ;

public class FirstActivity extends Activity{

  private Button button;
  private int count = 30;
  private int COUNT_TIME = 0;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.first_activity);
    button = (Button) findViewById();

  }

  private Handler handler = new Handler(){

    @Override
    public void handleMessage(Message msg) {
      (""+count);
      if(count <= 0){
        count = 30;
        (true);
        ("Re-time");
        (); //Cancel this task      }
    }
  };

  private Timer timer = new Timer();
  private TimerTask timerTask;
  public void clickButton(View view){
    (false);

    timerTask = new TimerTask() {

      @Override
      public void run() {
        count--;
        (COUNT_TIME);
      }
    };
    (timerTask,0,1000); //After 0 seconds, the task will be executed every second after  }

  @Override
  protected void onDestroy() {
    ();
    //Release resources    if(timerTask != null){
      ();
      timerTask = null;
    }
    if(timer != null){
      ();
      timer = null;
    }
  }
}

3. Use the native countdown class CountDownTimer that comes with Android

import ;
import ;
import ;
import ;
import ;

public class FirstActivity extends Activity{

  private Button button;

  private CountDownTimer timer;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.first_activity);
    button = (Button) findViewById();

  }

  public void clickButton(View view){
    (false);
    //The first parameter: the countdown milliseconds; the second parameter: the time interval for receiving onTick callback    timer = new CountDownTimer(30000, 10) {
       public void onTick(long millisUntilFinished) {
         (millisUntilFinished / 1000 + "Second");
       }
       public void onFinish() {
         ("Re-time");
         (true);
       }
     };
    ();
  }
  @Override
  protected void onDestroy() {
    ();
    if(timer != null){
      ();
    }
  }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.