从SMS - Android中提取OTP(6位数)

blo*_*ala 3 sms android

我有一个收听短信的广播接收器.当短信到达时,我有全文,但只关心OTP.

我的挑战是如何提取6位otp.我不能使用正则表达式,因为短信格式可能会改变.

示例"感谢您注册您的otp是123456"

我想要123456.短信结构可以改变,但otp总是一个6位数字

blo*_*ala 12

得到了这个使用模式和匹配器.

如果这有助于任何人(在onReceive回调广播接收器):

   //---This will match any 6 digit number in the message, can use "|" to lookup more possible combinations

            public Pattern p = Pattern.compile("(|^)\\d{6}");

  //---retrieve the SMS message received---

                try{
                       /**Extract sms*/
                        Object[] pdus = (Object[]) bundle.get("pdus");
                        msgs = new SmsMessage[pdus.length];
                        for(int i=0; i<msgs.length; i++)   //Msg Read
                        {
                            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                            msg_from = msgs[i].getOriginatingAddress();
                            msgBody = msgs[i].getMessageBody();
                        }

                    /*
                    * Now extract the otp*/
                        if(msgBody!=null) 
                          {
                            Matcher m = p.matcher(msgBody);
                            if(m.find()) {
                                otp.setText(m.group(0));
                               }
                            else
                              {
                               //something went wrong
                              }
                          }
                  }catch(Excep...
Run Code Online (Sandbox Code Playgroud)