Android中倒计时跳转的两种实现方式
- `Handler().postDelayed方式设定延时操作
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
}
}, 1000 * 4);`
- CountDownTimer类的使用,参数1 millisInFuture:倒计时的总数,单位毫秒;参数2 countDownInterval:表示间隔多少毫秒,调用一次onTick方法();
CountDownTimer countDownTimer = new CountDownTimer(4000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
splash_time_tv.setText("倒计时(" + millisUntilFinished / 1000 + ")");
}
@Override
public void onFinish() {
splash_time_tv.setText("开始跳转……");
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
}
};
countDownTimer.start();