The previous use of the countdown is written by their own, through the implementation of handler, only to find that the Android OS has implemented the tool class. Simple to use as follows. 1 import android.app.Activity;
2 ImportAndroid.os.Bundle;3 ImportAndroid.os.CountDownTimer;4 5 Public classCountdowntimeractivityextendsActivity {6 PrivateButton Mbtn_getcode;//User-selectable button-"Get Verification Code"7 PrivateCountdowntimer Mcountdowntimer;//countdown Tool class for sending verification codes8 9 @OverrideTen protected voidonCreate (Bundle savedinstancestate) { One Super. OnCreate (savedinstancestate); A Setcontentview (r.layout.activity_countdowntimer); -Mbtn_getcode =Findviewbyid (r.id.btn_getcode); - //Countdown Tool Class: Stop after 30 seconds with a 1-second interval theMcountdowntimer =NewCountdowntimer (30000, 1000) { - - @Override - Public voidOnTick (Longmillisuntilfinished) { + //millisuntilfinished will be from 30000,29000 ... until 0. -Btn_getprov.settext ("Resend (" + (millisuntilfinished/1000) + "s)"); + } A at @Override - Public voidonfinish () { - //once the countdown is over, the user can click the button again--"Get Verification Code" -Btn_getprov.settext ("Get Verification Code"); -Btn_getprov.setenabled (true); - } in };
Turn on Countdown
Btn_getprov.setenabled (FALSE);
Mcountdowntimer.start ();
- }
Android--countdowntimer Countdown Tool Class