I have a Forgot Password page. i want to when the otp message comes it reads the otp number and verify.But in my case OTP message reads but i want only OTP number. Like example My OTP message is :- 9563 is your OTP for Reset Password for your app. Treat this as a confidential. i read this message by the help of this class.but i want to get only OTP number like (9563).not the whole message.Can anyone tell me how can i do this ??
This is the class by which i read the otp message.
public class IncominMsg extends BroadcastReceiver {
final SmsManager smsManager = SmsManager.getDefault();
Context mContext;
Object[] pdusObj;
String message;
@Override
public void onReceive(Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
pdusObj = (Object[]) bundle.get("pdus");
SmsMessage[] smsMessages = new SmsMessage[pdusObj.length];
for (int i = 0; i < pdusObj.length; i++) {
smsMessages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
message = smsMessages[i].getDisplayMessageBody();
Log.e("message..", message);
}
Pattern generalOtpPattern = Pattern.compile(message);
Matcher generalOtpMatcher = generalOtpPattern.matcher(smsMessages[0].getMessageBody().toString());
if (generalOtpMatcher.find()) {
String otp = generalOtpMatcher.group(1);//this is only your OTP code
}
}
} catch (Exception e) {
}
}
}
I get the message and print in the logcat.but I want only OTP number not the whole message.