Android realizes automatic SMS Verification code filling function _android

Source: Internet
Author: User
Tags stringbuffer

This example for you to share the Android implementation of SMS Verification code automatic function, for your reference, the specific content as follows

The realization of the idea is very simple:

    • 1, in the need to enter the verification Code activity Code registration listening to the broadcast of SMS
    • 2, intercept SMS, get the verification code
    • 3, write back to EditText
Private Smsreciver Smsreciver = new Smsreciver (); 
  /** received SMS ACTION **/String action_sms_reciver = "Android.provider.Telephony.SMS_RECEIVED"; 
   /** * Registered broadcast recipient listens for SMS verification code automatically writeback can be registered in OnCreate (); 
    * * private void Registsmsreciver () {intentfilter filter = new Intentfilter (); 
    Filter.addaction (Action_sms_reciver); 
    Setting priority or not listening to SMS filter.setpriority (1000); 
    Registerreceiver (smsreciver, filter); /** * SMS Broadcast recipient users listen to SMS, automatically fill out the verification code/private class Smsreciver extends Broadcastreceiver {@Ove Rride public void OnReceive (context context, Intent Intent) {object[] Objs = (object[)) Intent.getextras (). GE 
       T ("PDUs"); 
         for (Object Obj:objs) {yte[] PDU = (byte[]) obj; 
         Smsmessage SMS = SMSMESSAGE.CREATEFROMPDU (PDU); 
         SMS content String message = Sms.getmessagebody (); 
        LOG.D ("Log", "message" + message); Short-term mobile phone number, if your company sent the verification code of the number is fixed here can be a number of calibration StrinG from = Sms.getoriginatingaddress (); 
         LOG.D ("Log", "from" + from); 
 
      Analysisverify (message);  }}/** * parsing SMS and write back here is a pure digital text message, if the small partner's verification code contains letters, can be replaced with the * * @param message/private 
     void Analysisverify (String message) {char[] msgs = Message.tochararray (); 
    StringBuffer sb = new StringBuffer (); for (int i = 0; i < msgs.length i++) {if (' 0 ' <= msgs[i] && msgs[i] <= ' 9 ') {sb. 
      Append (Msgs[i]); 
   } metverifycode.settext (Sb.tostring ()); 
    } @Override protected void OnDestroy () {Super.ondestroy (); 
      Cancel SMS Broadcast Registration if (Smsreciver!= null) {unregisterreceiver (smsreciver); 
    Smsreciver = null;  } 
  }

You can see that the code logic is relatively simple, there are several points to note. We use the code here to register the broadcast, the reason why we do not take global broadcasting in the form of two days, in the high version of the API, the registration of global SMS monitoring will be invalidated. And in terms of business, we listen to SMS only in the input of the activity of the verification code will be used, in the form of code registration, in the current activity destroyed when the cancellation of broadcast registration, more in line with our expectations, improve the performance of the application.

The second issue to be noted is the priority issue.

Filter.setpriority (1000); 

As you can see, we set the priority here to be the largest. Ensure that our applications can receive as much text as possible. Note that I am using "as much as possible", that is, we can not guarantee that the text can be automatically filled out to perform successfully, a small partner may ask, we do not set the priority to the highest level?

Why can't we guarantee it?

The reason is actually very simple, you can set the priority of listening to the message to the maximum, the same, other applications can be the priority of the SMS listening to set the maximum. For example, your mobile phone installed with 360 security guards, your company's verification code as a spam message interception, this time the SMS interception is invalid.

The above is the entire content of this article, I hope to help you learn.

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.