2013-10-20 150 views
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(); 
          } 
    } 
    } 
    } 
+0

你在比較字符串嗎?也許是編碼的東西? –

+0

請提供您的代碼在哪裏比較值 –

+0

是啊...我比較兩個字符串...詳細說明它如何可以編碼? –

回答

0

你不能比較字符串這樣的:

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

你應該使用:

msgBody.trim().equals(msgFrom.trim()) 
+0

這實際上工作。您。是。真棒。非常感謝! –

+0

很高興幫助:) –

相關問題