SoFunction
Updated on 2025-04-11

Android CountDownTimer implements timer and countdown effects

This article shares the specific code for Android implementation timer and countdown for your reference. The specific content is as follows

I believe you can understand it by directly uploading the code.

Android has already packaged a class, but many people don’t know it.

Code:

public class SplashActivity extends BaseAppCompatActivity { 
 
  @InjectView() 
  ImageView ivBg; 
  @InjectView() 
  TextView tvSkip; 
 
  int[] imgs = new int[]{ 
      , 
      , 
      , 
      , 
      }; 
 
  private CountDownTimer timer; 
 
  @Override 
  protected int getContentViewLayoutID() { 
    return .activity_splash; 
  } 
 
  @Override 
  protected void initViewsAndEvents() { 
    int index = (int) (() * ); 
 
    (imgs[index]); 
 
    timer = new CountDownTimer(3500, 1000) { 
      @Override 
      public void onTick(long millisUntilFinished) { 
        ((getResources().getString(), (int) (millisUntilFinished / 1000 + 0.1))); 
      } 
 
      @Override 
      public void onFinish() { 
        ((getResources().getString(), 0)); 
        startActivity(new Intent(mContext, )); 
        finish(); 
      } 
    }; 
    (); 
  } 
 
  @OnClick() 
  public void skip() { 
    if (timer != null) 
      (); 
 
    startActivity(new Intent(mContext, )); 
    finish(); 
  } 
 
  @Override 
  protected void onDestroy() { 
    (); 
 
    if (timer != null) { 
      (); 
    } 
  } 
} 

It's very simple when calling:();

Finally, let’s explain: CountDownTimer timer = new CountDownTimer(3500, 1000), the first parameter represents the total time, and the second parameter represents the interval time. It means that the method onTick will be called back every second, and then the onFinish method will be called back 10 seconds later.

Layout activity_splash.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:andro 
  android:layout_width="match_parent" 
  android:layout_height="match_parent"> 
 
  <ImageView 
    android: 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:scaleType="fitXY" /> 
 
  <TextView 
    android: 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:layout_margin="10dp" 
    android:background="@drawable/common_button_selector" 
    android:padding="5dp" 
    android:text="@string/skip" /> 
 
</RelativeLayout> 

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.