Not able to compare two string in android

2.1k views Asked by At

I'm new to Android. I'm developing an app which blocks unwanted calls. Now I'm stuck here when I try to compare the incoming number with the numbers in contact. Here is the code. Please help. Here while checking a Checkbox I need to block all calls from strangers (not in the contact)

Bundle extra=intent.getExtras();//new
telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if(extra!=null)//new=== getting the blocked number {
    state=extra.getString(telephonyManager.EXTRA_STATE);
    if(state.equals(telephonyManager.EXTRA_STATE_RINGING)) {
        number=extra.getString(telephonyManager.EXTRA_INCOMING_NUMBER);
        Log.w("INCOMMING NUMBER",number);
    }
}
if(noStrangers_cb.isChecked()){
    Cursor phones1 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
    while (phones1.moveToNext()){
        String phoneNumber = phones1.getString(phones1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        Log.d("NUMBER IN CONTACT",phoneNumber );

        //boolean val= number== phoneNumber;
        //String no=number;
        //Log.d("ASSIGNING NUMBER TO NO = ", no);
        //Collator c=Collator.getInstance();
        if(!phoneNumber.equals(number))
            {
            Log.d("IF ", "STRANGERS"+number);
            //Log.d("NUMBER CHECKING", "NUMBER = "+number+"CONTACT = "+phoneNumber);
            try {
                telephonyService.endCall();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
    }
    phones1.close();
}//end if Strangers

LOGCAT: enter image description here

Updated code is below:

if(allContacts_cb.isChecked())
                     {
                      Log.d("BLOCK ALL CONTACTS","blocking contacts.........." );
                      Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
                       while (phones.moveToNext())
                       {
                        String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        Log.d("NUMBER IN CONTACT ",phoneNumber );
                        String num=phoneNumber.replace("-","");
                        String incom=incomingNumber;
                        String s1="0"+num;
                        String s2="+91"+num;
if((num.equals(incomingNumber))||((s1).equals(incomingNumber))||(s2).equals(incomingNumber))//if(phoneNumber.compareTo(no)==0)//&&(checking==true))
                        { 
                          Log.d("IF ", "INSIDE IF OF BLOCKING CONTACTS");//+cn+"num"+n);
                          Log.d("NUMBER CHECKING", "NUMBER = "+number+"CONTACT = "+phoneNumber);
                          try {
                            ending1=telephonyService.endCall();
                            if(ending1)
                             {  
                             for(int i=1;i<=1;i++)
                             {
                              android.telephony.SmsManager sms = android.telephony.SmsManager.getDefault();
                              sms.sendTextMessage(incom, null, SMS, null, null);
                              break;
                             }
                              }
                              } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                                }

                          } 
                        else
                        {
                            if(noStrangers_cb.isChecked())
                            {
                                try {
                                    telephonyService.endCall();
                                } catch (RemoteException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }
                        }
                          }//end of while
                          phones.close();
                         }//end if allContacts

Can anyone help me.. Please..

3

There are 3 answers

8
Pankaj Kumar On BEST ANSWER

Replacing _ character from your number is not your final solution. You should read PhoneNumberUtils, or here is best library 'libphonenumber' which can be used for various type of operations related to phone numbers.

Read details about libphonenumber.

And below is code snipped which can be used for your purpose.

public boolean isCallerInDatabase(String caller, String phoneNumberFromDb) {
        phoneUtil = PhoneNumberUtil.getInstance();
        if(phoneUtil.isNumberMatch(caller, phoneNumberFromDb) != PhoneNumberUtil.MatchType.NO_MATCH) {
            //Number matched 
            Log.i("TEST", "NUMBER MATCHED");
            return true;
        }
        Log.i("TEST", "NUMBER NOT MATCHED");
        return false;       
    }

Update: Android Added that library as Utility class.

Android itself has been added PhoneNumberUtils class for phone related helper methods. To compare two Phone Numbers you should use PhoneNumberUtils.compare method.

Happy coding :)

8
subbu On

You can replace the string "-" with "" in your phone number and then use that string to compare.

string no= Phonecontactno.StringReplace("-","");

Now use this string to compare with incoming no.

3
user1208720 On

try the following condition may be it helps you

if(!(phoneNumber.toString().equals(number.toString)))