Skip to content

Instantly share code, notes, and snippets.

@kmerrell42
Last active April 18, 2017 20:50
Show Gist options
  • Save kmerrell42/e02c07fdbf882551e3594d7c99b5870b to your computer and use it in GitHub Desktop.
Save kmerrell42/e02c07fdbf882551e3594d7c99b5870b to your computer and use it in GitHub Desktop.
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