Search code examples
javaandroidif-statementtelephonymanager

Adding the if condition quits the android app in telephony manager


I am trying to make a simple android app in which there is a toast message containing the number of the incoming call. I have stored the number in a string variable. When I display just the toast, it works fine but before the toast, if I add a simple if condition comparing the number to another string, the app quits. The required permissions are given. Can someone help me?

This Works:

public void onReceive(Context context, Intent intent) {

        String incomingNumber = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Toast.makeText(context, incomingNumber, Toast.LENGTH_LONG).show();
    }

This Does not Work (App quits)

public void onReceive(Context context, Intent intent) {

        String incomingNumber = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
          if(incomingNumber.equals("+919999999999"))
           {
              Toast.makeText(context, "Call from Mom", Toast.LENGTH_LONG).show();
           }
           else
           {
              Toast.makeText(context, incomingNumber, Toast.LENGTH_LONG).show();
           }

    }

Solution

  • It sounds a little silly but I just wrapped my "if" condition in a try-catch block and it solved the problem. I am not used to programming in JAVA so it took me a lot to figure out this simple thing. Thank You All for providing me with suggestions :)