Android realizes the countdown function based on CountDownTimer

  • 2020-11-18 06:26:56
  • OfStack

An example of Android programming based on CountDownTimer is presented in this paper. To share for your reference, the details are as follows:

When browsing the forum, I saw a netizen asking a question and mentioned the CountDownTimer class. You can see it from the name above and record the download time. Encapsulates the creation of the background thread and the Handler queue into a convenient class call.

This class is extremely simple, with only four methods, all of which involve onTick, onFinsh, cancel, and start. The first two are abstract methods, so override 1.

Here's a little official example:


new CountdownTimer(30000, 1000) {
  public void onTick(long millisUntilFinished) {
    mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
  }
  public void onFinish() {
    mTextField.setText("done!");
  }
}.start();

Direct use of the netizen's code, oneself slightly changed 1, a simple small demo.


package cn.demo;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.os.CountDownTimer;
import android.widget.TextView;
import android.widget.Toast;
public class NewActivity extends Activity {
  private MyCount mc;
  private TextView tv;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tv = (TextView)findViewById(R.id.show);
    mc = new MyCount(30000, 1000);
    mc.start();
  }//end func
  /* define 1 A countdown of internal classes */
  class MyCount extends CountDownTimer {
    public MyCount(long millisInFuture, long countDownInterval) {
      super(millisInFuture, countDownInterval);
    }
    @Override
    public void onFinish() {
      tv.setText("finish");
    }
    @Override
    public void onTick(long millisUntilFinished) {
      tv.setText(" Please wait for 30 seconds (" + millisUntilFinished / 1000 + ")...");
      Toast.makeText(NewActivity.this, millisUntilFinished / 1000 + "", Toast.LENGTH_LONG).show();//toast There is a display time delay 
    }
  }
}

The main thing is to rewrite onTick and onFinsh. The code in onFinish() is what you do when the timer ends. The code in onTick(Long m) is what you do at the beginning of your countdown, the parameter m is the time until completion, and the two parameters in constructor MyCount(), the former is the number of seconds backward and the latter is the interval between the countdown onTick event responses, both in milliseconds. For example, to count down 30 seconds, with an interval of 1 second per second, the two parameters can be MyCount(30000,1000). The creation of the background thread and the Handler queue are encapsulated as a convenient class call.

Use the mc.cancel () method when you want to cancel.

I hope this article has been helpful for Android programming.


Related articles: