0

我试图通过将文本发送到用户的手机然后获取它并匹配用户提供的号码和文本刚刚来自的号码来确认用户的电话号码......问题是虽然文本匹配 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();
                            }
   }
   }
   }
4

1 回答 1

0

你不能像这样比较字符串:

msgBody.trim() == msgFrom.trim()

你应该使用:

msgBody.trim().equals(msgFrom.trim())
于 2013-10-20T13:20:49.700 回答