Summary of common methods for Android to realize timing and countdown

  • 2020-09-28 09:08:45
  • OfStack

This paper summarizes the common methods of Android to realize timing and countdown. To share for your reference, the details are as follows:

Method 1

Timer and TimerTask (Java implementation)


public class timerTask extends Activity{ 
  private int recLen = 11; 
  private TextView txtView; 
  Timer timer = new Timer(); 
  public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.timertask); 
    txtView = (TextView)findViewById(R.id.txttime); 
    timer.schedule(task, 1000, 1000);    // timeTask 
  }   
  TimerTask task = new TimerTask() { 
    @Override 
    public void run() { 
      runOnUiThread(new Runnable() {   // UI thread 
        @Override 
        public void run() { 
          recLen--; 
          txtView.setText(""+recLen); 
          if(recLen < 0){ 
            timer.cancel(); 
            txtView.setVisibility(View.GONE); 
          } 
        } 
      }); 
    } 
  }; 
}

Method 2

TimerTask and Handler (modified version without Timer)


public class timerTask extends Activity{ 
  private int recLen = 11; 
  private TextView txtView; 
  Timer timer = new Timer(); 
  public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.timertask); 
    txtView = (TextView)findViewById(R.id.txttime); 
    timer.schedule(task, 1000, 1000);    // timeTask 
  }   
  final Handler handler = new Handler(){ 
    @Override 
    public void handleMessage(Message msg){ 
      switch (msg.what) { 
      case 1: 
        txtView.setText(""+recLen); 
        if(recLen < 0){ 
          timer.cancel(); 
          txtView.setVisibility(View.GONE); 
        } 
      } 
    } 
  }; 
  TimerTask task = new TimerTask() { 
    @Override 
    public void run() { 
      recLen--; 
      Message message = new Message(); 
      message.what = 1; 
      handler.sendMessage(message); 
    } 
  }; 
}

Methods 3

Handler and Message (not TimerTask)


public class timerTask extends Activity{ 
  private int recLen = 11; 
  private TextView txtView; 
  public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.timertask);  
    txtView = (TextView)findViewById(R.id.txttime); 
    Message message = handler.obtainMessage(1);   // Message 
    handler.sendMessageDelayed(message, 1000); 
  }  
  final Handler handler = new Handler(){ 
    public void handleMessage(Message msg){     // handle message 
      switch (msg.what) { 
      case 1: 
        recLen--; 
        txtView.setText("" + recLen); 
        if(recLen > 0){ 
          Message message = handler.obtainMessage(1); 
          handler.sendMessageDelayed(message, 1000);   // send message 
        }else{ 
          txtView.setVisibility(View.GONE); 
        } 
      } 
      super.handleMessage(msg); 
    } 
  }; 
}

Methods 4

Handler and Thread (no UI threads)


public class timerTask extends Activity{ 
  private int recLen = 0; 
  private TextView txtView; 
  public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.timertask); 
    txtView = (TextView)findViewById(R.id.txttime); 
    new Thread(new MyThread()).start();     // start thread 
  }   
  final Handler handler = new Handler(){     // handle 
    public void handleMessage(Message msg){ 
      switch (msg.what) { 
      case 1: 
        recLen++; 
        txtView.setText("" + recLen); 
      } 
      super.handleMessage(msg); 
    } 
  }; 
  public class MyThread implements Runnable{   // thread 
    @Override 
    public void run(){ 
      while(true){ 
        try{ 
          Thread.sleep(1000);   // sleep 1000ms 
          Message message = new Message(); 
          message.what = 1; 
          handler.sendMessage(message); 
        }catch (Exception e) { 
        } 
      } 
    } 
  } 
}

Methods 5

Handler and Runnable (simplest)


public class timerTask extends Activity{ 
  private int recLen = 0; 
  private TextView txtView; 
  public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.timertask); 
    txtView = (TextView)findViewById(R.id.txttime); 
    handler.postDelayed(runnable, 1000); 
  }   
  Handler handler = new Handler(); 
  Runnable runnable = new Runnable() { 
    @Override 
    public void run() { 
      recLen++; 
      txtView.setText("" + recLen); 
      handler.postDelayed(this, 1000); 
    } 
  }; 
}

Timing and counting down

Method 1, method 2, and method 3 are all countdowns
Method four, method five, they're all timing
Timing and countdown, both of which can be implemented using the above method (with minor changes to the code)

UI thread comparison

Methods 1, 2, and 3 are all timed by the UI thread implementation;
Methods 4 and 5 are timed by another Runnable thread

Implementation mode comparison

In method 1, Java is adopted, namely Timer and TimerTask.
The other four methods all use Handler message processing

It is recommended to use

If you don't have a high requirement for UI thread interaction, you can choose methods 2 and 3
If the UI thread is blocked and the user experience is seriously affected, it is recommended to use Method 4 and separate threads for timing and other logical processing
Method 5, which combines the advantages of the previous methods, is the simplest

I hope this article is helpful for Android programming.


Related articles: