Timer under Android programming

Source: Internet
Author: User

In the Android APP's mobile phone number registration logic, there will often be the activation code sent to the mobile phone link, the majority of applications in this link to take into account the network delay or server pressure, as well as the delay of the SMS service providers, will give the user a re-get Activation code button. As shown in the following:

Similarly, in order to prevent users from malicious frequent sending activation code, the application needs to send the user activation code time interval limit, then need to use the countdown device, the process is like this: when the page is initialized, the button is clickable state, the user clicked "Send Activation Code" button to become non-clickable state, At the same time the text on the button becomes a countdown, and after the countdown, the button becomes clickable and the text becomes "Send activation Code". The specific logic looks at the following code:

Package Cn.sunzn.countdown;import Android.app.activity;import Android.os.bundle;import Android.os.CountDownTimer; Import Android.view.menu;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;public class Mainactivity extends Activity implements Onclicklistener {private Timecount Timecou    nt    Private Button Btn_reget_captcha;        protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    Init ();        } private void Init () {Initview ();    InitData ();    } private void InitData () {timecount = new Timecount (60000, 1000);        } private void Initview () {Btn_reget_captcha = (Button) Findviewbyid (R.id.btn_reget_captcha);    Btn_reget_captcha.setonclicklistener (this);        public boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu);    return true; } public void OnClick (View v) {int id = V.getid ();  Switch (ID) {Case r.id.btn_reget_captcha:if (btn_reget_captcha.isclickable ())} {//TODO            Run your logic that you want to do Timecount.start ();        } break;        Default:break;            }} class Timecount extends Countdowntimer {public Timecount (long millisinfuture, long Countdowninterval) {        Super (Millisinfuture, countdowninterval);            } public void OnFinish () {btn_reget_captcha.settext ("Send Activation Code");        Btn_reget_captcha.setclickable (TRUE);            public void OnTick (long millisuntilfinished) {btn_reget_captcha.setclickable (false);        Btn_reget_captcha.settext ("Click on the re-send activation code after" + millisuntilfinished/1000 + "seconds); }    }}

Finally, attach the engineering code: Countdown

Timer under Android programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.