Home » Android » The effect of the countdown

The effect of the countdown

android countdown to the use of


1., for example, how many seconds after the delay, and then perform an action, such as from the flash screen display 2 seconds after entering the home page



private, void, goHomeActivity () {
mTimer, =, new, Timer (), ;
mTimer.schedule (new, TimerTask () {
@Override
public, void, run () {
StartActivity (new, Intent (SplashActivity.this, HomeActivity.class)) ;
finish () ;
}
}, 2000) ;
}






but it has a little problem, in the end is also no display of the splash screen, if the user presses the back button, the program still performs the countdown to the inside of the code, such as the example below, still will enter into HomeActivity, the solution is as follows:



@Override
public, void, onBackPressed () {
timer.cancel () // when the user presses the return key on the splash page, the HomeActivity is not entered
super.onBackPressed () ;
}


2. send verification code and so on, need 60 seconds countdown, the following countdown source code is as follows:


countdown to 60 seconds, from 60 seconds countdown, need to count the place to call startTimer ();



 private, void, startTimer () {
if, (null, = = mTimer) {
MTimer.cancel () ;
}
MTimer = new, Timer (), ;
mTimer.schedule (new, TimerTask () {
int, curSecond, =, 60;

@Override
public, void, run () {
RunOnUiThread (new, TimerTask () {
@Override
public, void, run () {
if, (curSecond, <=, >0, MGetVerifyCodeButton.setText ( get the validation code ) > ;
mGetVerifyCodeButton.setEnabled (true) ;
this.cancel () ;
} else {
MGetVerifyCodeButton.setText (curSecond, +, , ), ;
mGetVerifyCodeButton.setEnabled (false) ;
curSecond--;
}
}
} ;

}
}, 0, 1000) ;
}




note:


 in the onDestory method
If (null = = mTimer) {
MTimer.cancel ();
MTimer = null;
}

Latest