0

My android app first displays a toast when a message is received and when it receives a message from a particular number it shows another toast. But it is not displaying the second toast.

Here is my code:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;


public class IncomingSms extends BroadcastReceiver {

    // Get the object of SmsManager
    final SmsManager sms = SmsManager.getDefault();

    public void onReceive(Context context, Intent intent) {

        // Retrieves a map of extended data from the intent.
        final Bundle bundle = intent.getExtras();

        try {

            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);

                    int duration = Toast.LENGTH_SHORT;
                    Toast toast = Toast.makeText(context, "senderNum: "+ senderNum + ", message: " + message, duration);
                    toast.show();
                    String serverNumber= "+919886096376";
                    if(senderNum == serverNumber)
                    {
                        Toast toast1 = Toast.makeText(context,"alert message received!!!!",Toast.LENGTH_LONG);
                        toast.show();
                    }

                } // end for loop
              } // bundle is null

        } catch (Exception e) {
            Log.e("SmsReceiver", "Exception smsReceiver" +e);

        }
    }



}

2 Answers2

3

You are comparing if(senderNum == serverNumber) in wrong way. In Java & Android == is used to compare objects not Strings. When you want to compare two Strings you need to use .equals() method.

You need to compare like below,

if(senderNum.equals(serverNumber))
{
      Toast toast1 = Toast.makeText(context,"alert message received!!!!",Toast.LENGTH_LONG);
      toast1.show();     // change this to toast1
}
Lucifer
  • 29,392
  • 25
  • 90
  • 143
1

Change this

if(senderNum == serverNumber){
    Toast toast1 = Toast.makeText(context,"alert message received!!!!",Toast.LENGTH_LONG);
    toast.show();
}

to this

if(senderNum.equals(serverNumber)){
    Toast toast1 = Toast.makeText(context,"alert message received!!!!",Toast.LENGTH_LONG);
    toast1.show();
}

You are not calling show on the correct toast.

Also, the proper way to compare 2 Strings is the following :

senderNum.equals(serverNumber)

By using the == operator, you are comparing references to String objects, so it will much likely return false.

2Dee
  • 8,609
  • 7
  • 42
  • 53