Last active
April 18, 2017 20:50
-
-
Save kmerrell42/e02c07fdbf882551e3594d7c99b5870b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SmsBroadcastReceiver extends BroadcastReceiver { | |
... | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
final Bundle bundle = intent.getExtras(); | |
if (bundle != null) { | |
final Object[] pdusObj = (Object[]) bundle.get("pdus"); | |
if (pdusObj != null) { | |
for (Object aPdusObj : pdusObj) { | |
@SuppressWarnings("deprecation") // Need to support older Android SDKs | |
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) aPdusObj); | |
String senderNum = currentMessage.getDisplayOriginatingAddress(); | |
Matcher expectedNumberMatcher = Constants.EXPECTED_SENDER_NUMBER_PATTERN.matcher(senderNum); | |
if (expectedNumberMatcher.matches()) { | |
String message = currentMessage.getDisplayMessageBody(); | |
// Parse out the code from the message | |
Matcher matcher = Constants.VERIFICATION_CODE_PATTERN.matcher(message); | |
if (matcher.matches()) { | |
codeReceivedListener.onVerificationCodeReceived(matcher.group(1)); // group 1 is the first group extracted from the entire match | |
} | |
} | |
} | |
} | |
} | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment