Android uses the SMS verification code for Automatic Broadcasting and Android SMS verification code for Backfilling.

Source: Internet
Author: User

Android uses the SMS verification code for Automatic Broadcasting and Android SMS verification code for Backfilling.

Every time we register an account for any software, we generally need to enter

The verification code can be viewed only after receiving the text message.

The problem arises. After receiving the SMS, we can enter the Verification Code directly.

Enter in the input box.

Next, paste the code.

Declaration

Private BroadcastReceiver smsReceiver; // defines a broadcast receiver private IntentFilter filter2; // private EditText editCaptcha; // The text message is automatically filled in with the private String strContent; // The Verification Code content is private String patternCoder = "(? <! \ D) \ d {4, 10 }(?! \ D) "; // verify the text message content


Code Implementation

Private Handler handler = new Handler () {public void handleMessage (android. OS. message msg) {editCaptcha. setText (strContent) ;};}; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); editCaptcha = (EditText) findViewById (R. id. login_editcaptcha); filter2 = new IntentFilter (); // create an intent filter filter2.addAction ("android. provider. T Elephony. SMS_RECEIVED "); // create an intent action filter2.setPriority (Integer. MAX_VALUE); // sets the maximum level of smsReceiver = new BroadcastReceiver () {@ Overridepublic void onReceive (Context context, Intent intent) {Object [] objs = (Object []) intent. getExtras (). get ("pdus"); // get the received message for (Object obj: objs) {byte [] pdu = (byte []) obj; SmsMessage sms = SmsMessage. createFromPdu (pdu); // message content String message = sms. getMessageBody (); // get Take the text message content Log. d ("logo", "message" + message); // the text message .. Start with + 86? String from = sms. getOriginatingAddress (); // get the mobile phone number Toast. makeText (getApplicationContext (), "from:" + from, Toast. LENGTH_LONG ). show (); Log. d ("logo", "from" + from); if (! TextUtils. isEmpty (from) {// judge whether the number is empty String code = patternCode (message); if (! TextUtils. isEmpty (code) {strContent = code; handler. sendEmptyMessage (1) ;}}}}; registerReceiver (smsReceiver, filter2); // register the broadcast listener} @ Overrideprotected void onDestroy () {super. onDestroy (); // cancel the automatically entered broadcast unregisterReceiver (smsReceiver);}/*** match the six numbers in the middle of the text message (Verification Code, etc) ** @ param patternContent * @ return */private String patternCode (String patternContent) {if (TextUtils. isEmpty (patternContent) {return null;} Pattern p = Pattern. compile (patternCoder); Matcher matcher = p. matcher (patternContent); if (matcher. find () {return matcher. group ();} return null ;}

In addition, we need to grant permissions to the program list file.

<! -- SMS receiving permission --> <uses-permission android: name = "android. permission. RECEIVE_SMS"> </uses-permission>

Then our software can automatically repeat the data !!!

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.