Android SMS verification code control and android verification code control

Source: Internet
Author: User

Android SMS verification code control and android verification code control

Android SMS verification code control, easy to use unified style in projects, unified prompt modification, I personally think it is quite useful

<Span style = "font-size: 18px;"> public class SMSButton extends Button {/** whether to display */private boolean mAttached; /** wait time after the verification code is sent */private int delayTime = 60;/** text above the Button */private String showText = "get "; /** text displayed when the Button is waiting */private String delayText = FORMATETAG + "second";/** whether the countdown is now */private boolean isDelayed = false; private SMSOnClickListener clickListener; private int currentDelayTime = 0; private s Tatic final String FORMATETAG = "% d"; private Runnable mTimer = new Runnable () {@ Overridepublic void run () {currentDelayTime-= 1; if (currentDelayTime <= 0) {isDelayed = false; setClickable (true); setText (showText);} else {getHandler (). postDelayed (mTimer, 1000); setDelayTimeText (currentDelayTime) ;}}; private void setDelayTimeText (int time) {setText (String. format (delayText, time);} public SMSButton (Con Text context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); initView ();} public SMSButton (Context context, AttributeSet attrs) {super (context, attrs); initView ();} public SMSButton (Context context) {super (context); initView () ;}@ Overridepublic void setOnClickListener (OnClickListener l) {super. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {if (! IsDelayed) {// if you are not waiting for if (clickListener! = Null) {if (clickListener. validate () {setClickable (false); isDelayed = true; currentDelayTime = delayTime; getHandler (). post (mTimer); clickListener. onClick () ;}}}});}/*** initialization page */private void initView () {String text = getText (). toString (). toString (). trim (); if (text! = Null &&! "". Equals (text) {showText = text ;}@ Overrideprotected void onAttachedToWindow () {super. onAttachedToWindow (); if (! MAttached) {mAttached = true; setText (showText) ;}@overrideprotected void onDetachedFromWindow () {super. onDetachedFromWindow (); if (mAttached) {mAttached = false; isDelayed = false; getHandler (). removeCallbacks (mTimer) ;}} public int getDelayTime () {return delayTime;} public void setDelayTime (int delayTime) {this. delayTime = delayTime;} public String getShowText () {return showText;} public void setShowText (String showText) {this. showText = showText;} public String getDelayText () {return delayText;} public void setDelayText (String start, String end) {this. delayText = start + FORMATETAG + end;} public SMSOnClickListener getSMSOnClickListener () {return clickListener;} public void setSMSOnClickListener (SMSOnClickListener clickListener) {setOnClickListener (null); this. clickListener = clickListener;} public interface SMSOnClickListener {public boolean validate (); public void onClick () ;}</span>
Usage:

/*** Example ** smsButton = (SMSButton) findViewById (R. id. smsButton); * smsButton. setShowText ("get"); * smsButton. setDelayText ("", "seconds"); smsButton. setSMSOnClickListener (new * SMSOnClickListener () {** @ Override public boolean validate () {return true;} * @ Override public void onClick () {* ToastUtil. getInstance (). toastInCenter (LoadingActivity. this, * "onClick ");}});*/




How does the android script genie obtain the SMS verification code?

The automated test of android applications will inevitably involve the login registration function, while many login registration or password modification functions often require text message verification code, so it is necessary to automatically obtain the issued text message verification code.
The main function is to obtain text message information in real time.
The main methods for obtaining text message information on android are BroadcastReceiver and database. If you want to obtain text message information in real time, BroadcastReceiver is more convenient.
Public class SMSReceiver extends BroadcastReceiver {
Private String verifyCode = "";
Public static final String TAG = "SMSReceiver ";
Public static final String SMS_RECEIVED_ACTION = "android. provider. Telephony. SMS_RECEIVED ";
@ Override
Public void onReceive (Context context, Intent intent ){
If (intent. getAction (). equals (SMS_RECEIVED_ACTION )){
SmsMessage [] messages = getMessagesFromIntent (intent );
For (SmsMessage message: messages ){
Log. I (TAG, message. getOriginatingAddress () + ":" +
Message. getDisplayOriginatingAddress () + ":" +
Message. getDisplayMessageBody () + ":" +
Message. getTimestampMillis ());
String smsContent = message. getDisplayMessageBody ();
Log. I (TAG, smsContent );
WriteFile (smsContent); // write the text message content to the SD card
}
}
}
Public final SmsMessage [] getMessagesFromIntent (Intent intent ){
Object [] messages = (Object []) intent. getSerializableExtra ("pdus ");
Byte [] [] pduObjs = new byte [messages. length] [];
For (int I = 0; I <messages. length; I ++)
{
PduObjs [I] = (byte []) messages [I];
}
Byte [] [] pdus = new byte [pduObjs. length] [];
Int pduCount = pdus. length;
SmsMessage [] msgs = new SmsMessage [pduCount];
For (int I = 0; I <pduCount; I ++ ){
Pdus [I] = pduObjs [I];
Msgs [I] = smsmesloud... the remaining full text>

The android client of China Construction Bank mobile phone bank will send a text message Verification Code during the transfer. After reading the text message verification code, the client needs to enter again. What should I do?

I don't know what type of mobile phone you are using. Generally, after a verification code is sent on a mobile phone, Android users can pull the text message up and down with their fingers to obtain the verification code, then, you can enter the verification code to perform operations on the mobile banking system.

Related Article

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.