1

I am making a project in which i am using the phone contacts . In this app i am matching the number( incoming call number) with the numbers saved in contact list. I have formated the number and it is working over the phones on which the number is saved with this format +919045308261 and its not working over the phones on which the number is saved with this format +91 90 45 308261 The code that I have used is...

int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor
                        .getColumnIndex(HAS_PHONE_NUMBER)));

                if (hasPhoneNumber > 0) {


                    Cursor phoneCursor = contentResolver.query(
                            PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?",
                            new String[] { contact_id }, null);

                    while (phoneCursor.moveToNext()) {


                        phoneNumber = phoneCursor.getString(phoneCursor
                                .getColumnIndex(NUMBER));
                        String formatedphn = PhoneNumberUtils.formatNumber(phoneNumber);
formatedphn = formatedphn.trim();
                        Log.i("formated number: ", formatedphn);
                //here the number is from which the call is incoming        
                        if(number.equals(formatedphn)){
                            Toast.makeText(getApplicationContext(), "got it", Toast.LENGTH_SHORT).show();
                            Log.i("Match occurs", "got a hit");
                            //Log.i("number= ", number);
                            String contname = phoneCursor.getString(phoneCursor
                                    .getColumnIndex(DISPLAY_NAME));
                            if(!contname.equals(null)){
                                Toast.makeText(getApplicationContext(), contname, Toast.LENGTH_SHORT).show();
                                tts.speak(contname+" calling", TextToSpeech.QUEUE_FLUSH, null);

                            }
                        }
nawaab saab
  • 1,892
  • 2
  • 20
  • 36

4 Answers4

1

String.trim() removes all leading and trailing spaces of a String, but not those in the middle. To remove all whitespaces of a String you can use this regex:

str = str.replaceAll("\\s+","");
Steve Benett
  • 12,843
  • 7
  • 59
  • 79
  • thanks it worked but could you please solve 1 more problem of mine, in different phone you know that the number +919045308261 is saved in diffrent ways like 0 90 45 308261, 9045308261,90 45 308261. How may i match these with +919045308261 from which the call is incoming – nawaab saab Apr 05 '14 at 08:02
  • This I don't know. Consider to open a new question to this problem. – Steve Benett Apr 05 '14 at 08:20
0

You can do the following thing, compare after removing all the white spaces from the string.

String st = " Hello world";
st = st.trim(); // It should work fine and remove all white spaces.

now compare two strings as you are comparing. All d best

karan
  • 8,637
  • 3
  • 41
  • 78
0

Here are some references:

Community
  • 1
  • 1
amalBit
  • 12,041
  • 6
  • 77
  • 94
0

According to This Thread How to format a phone number using PhoneNumberUtils? you can get formatted Number from +98 91 28 033921 to this 0912 80 33921 and compare with this

Community
  • 1
  • 1
Amin Bahiraee
  • 538
  • 10
  • 20