我正在尝试通过将文本发送到用户手机然后获取并匹配用户提供的号码和文本来自的数字来确认用户的电话号码...问题是虽然文本匹配100%,我已经在很多方面证实,字节数组不匹配,因此“电话号码”不匹配,并且无法确认。这是真的吗:通过网络发送的值改变字节数组?
发送的文字:
String user_phone_number = phoneNumber.getText().toString();
String number = "" + user_phone_number;
String verificationCode = "717345221";
String message = verificationCode + user_phone_number;
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(number, null, message, null, null);
收到的文字:
if (intent.getAction()
.equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String msgFrom;
if (bundle != null) {
try {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
msgFrom = msgs[i].getOriginatingAddress();
String msgBody = msgs[i].getMessageBody();
String verificationCode = "717345221";
if (msgBody.startsWith(verificationCode)) {
msgBody = msgBody.substring(verificationCode
.length());
if (msgBody.trim() == msgFrom.trim()) {
showCorrectNotification(context);
try {
Intent o = new Intent(context,
ProfileInformation.class);
o.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(o);
} catch (Exception e) {
e.toString();
}
}
}
}
答案 0 :(得分:0)
你不能比较这样的字符串:
msgBody.trim() == msgFrom.trim()
您应该使用:
msgBody.trim().equals(msgFrom.trim())