Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the countdown function of SMS CAPTCHA by Android

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "Android how to achieve SMS CAPTCHA countdown function", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Android how to achieve SMS CAPTCHA countdown function" article.

Scenario: when registering an account page, we click the button to send the CAPTCHA. When waiting for the CAPTCHA, the screen will have a countdown prompt. During this period, the button cannot be clicked. When the countdown is over, the button resumes.

Implementation and function are not difficult, this time using the RxBinding,RxJava2 method to achieve. And realize the manual and automatic stop countdown to prevent multiple clicks.

Functional dynamic diagram

To add a Gradle configuration using RxBinding and RxJava2:

Compile 'io.reactivex.rxjava2:rxandroid:2.0.1'compile' io.reactivex.rxjava2:rxjava:2.0.1'compile 'com.jakewharton.rxbinding2:rxbinding:2.0.0'compile' com.jakewharton.rxbinding2:rxbinding-support-v4:2.0.0'compile 'com.jakewharton.rxbinding2:rxbinding-appcompat-v7:2.0.0'

First bind and convert to a countdown Observable observer object through RxView.clicks ().

Observable mObservableCountTime = RxView.clicks (mBtnSendMsm) / / prevent repeated clicks. ThrottleFirst (MAX_COUNT_TIME, TimeUnit.SECONDS) / / convert the click event to a countdown event. FlatMap (new Function () {@ Override public ObservableSource apply (Object o) throws Exception {/ / update the status of the send button and initialize the display of the countdown text RxView.enabled (mBtnSendMsm) .accept (false) RxTextView.text (mBtnSendMsm) .accept ("remaining" + MAX_COUNT_TIME + "seconds"); / / in practice, you can send a request to get the network / / return the countdown observer object within N seconds. Return Observable.interval (1, TimeUnit.SECONDS, Schedulers.io ()) .take (MAX_COUNT_TIME);}}) / / replace the incrementing number with the decreasing countdown number.map (new Function () {@ Override public Long apply (Long aLong) throws Exception {return MAX_COUNT_TIME-(aLong + 1);}}) .takeon (AndroidSchedulers.mainThread ()); / / switch to the main thread of Android.

Sets the observed object of the Consumer as a countdown prompt.

Consumer mConsumerCountTime = new Consumer () {@ Override public void accept (Long aLong) throws Exception {/ / shows the remaining time. When the countdown is 0, restore the btn button. If (aLong = = 0) {RxView.enabled (mBtnSendMsm) .accept (true); RxTextView.text (mBtnSendMsm) .accept ("send CAPTCHA");} else {RxTextView.text (mBtnSendMsm) .accept ("remaining" + aLong + "seconds");}

Subscribe to click events:

/ / subscribe to click event Disposable mDisposable = mObservableCountTime.subscribe (mConsumerCountTime)

Stop the countdown, but you can still click again.

/ / reset the CAPTCHA button. RxView.clicks (mBtnClean) .subscribe (new Consumer () {@ Override public void accept (Object o) throws Exception {if (mDisposable! = null & &! mDisposable.isDisposed ()) {/ / stop the countdown mDisposable.dispose (); / / re-subscribe mDisposable = mObservableCountTime.subscribe (mConsumerCountTime); / / button can be clicked RxView.enabled (mBtnSendMsm) .accept (true) RxTextView.text (mBtnSendMsm) .accept ("send CAPTCHA");})

Destroys emptied data when exiting the current page.

@ Overrideprotected void onDestroy () {super.onDestroy (); if (mDisposable! = null) {mDisposable.dispose ();}} the above is about "how to realize the countdown function of SMS CAPTCHA in Android". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to learn more about related knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report