0

I'm developing an app where I'm comparing a received SMS text with and existing string variable if texts Match Toast pops up else doesn't Where am I doing wrong ?

public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    final Bundle bundle = intent.getExtras();

    final String pwd = "123";


    if(bundle != null)
    {

        final Object[] pdusObj = (Object[]) bundle.get("pdus");

        for (int i = 0; i < pdusObj.length; i++) {

            SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
            String phoneNumber = currentMessage.getDisplayOriginatingAddress();

            String senderNum = phoneNumber;
            String message = currentMessage.getDisplayMessageBody();


            Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
            if(message == pwd)
            {


           // Show Alert
            int duration = Toast.LENGTH_LONG;
            Toast toast = Toast.makeText(context, 
                         message, duration);
            toast.show();
           }
            else
            {
                 int duration = Toast.LENGTH_LONG;
                 Toast toast = Toast.makeText(context,  "Doesn't match", duration);
                 toast.show();
            }


        } // end for loop

But condition is not getting true .. always " else's " toast is showing!

1 Answers1

0

In case of comparing Strings use:

if(message.equals(pwd))

insted of:

if(message == pwd)
Rahul Sharma
  • 5,949
  • 5
  • 36
  • 46