9

I have to search a contact based on the phone number. Here is the code that works to fetch contacts. Android API level which I am using is 15

        String[] projection = new String[] {
                ContactsContract.CommonDataKinds.Phone._ID,
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.TYPE,
                ContactsContract.CommonDataKinds.Phone.NUMBER};

        Cursor query = mContent.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection,
                    ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE '%" + constraint.toString() + "%'"
                    ,null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");

I am able to fetch contact that doesn't have space with in the phone number saved in contacts table. If the phone number has a space between, the above query fails. For example, for a contact, if the phone number is saved as 1234567890 and when I am searching with value 1234, this contact is retrieved. But fails if the contact is saved as "123 456 7890".

Bottom line, when I try to search contacts that has or contains "1234" with phone number, the resulting should return me contacts with phone number "1234567890" and "123 4567 890". As some of the android phones saves phone numbers with space between.

How do I solve this. Any help is appreciated.

User12111111
  • 1,179
  • 1
  • 19
  • 41
  • Adding `%` between every characters in your `constraint` would be one option – venkatKA Sep 08 '14 at 10:37
  • 2
    Long story short, to search by phone number, you need to use `ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER` which contains the phone number in `E.164` format. http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.Phone.html#NORMALIZED_NUMBER – Sarwar Erfan Sep 08 '14 at 11:01
  • @SarwarErfan Sorry I cannot use this constant, the API level that I am on is 15. `ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER` is added in API 16. – User12111111 Sep 08 '14 at 11:06
  • for older api, check this: http://stackoverflow.com/questions/4579009/android-why-number-key-return-the-number-in-reverse-order/4583115#4583115 – Sarwar Erfan Sep 08 '14 at 12:10
  • Were you able to find any solution to this? – nipun.birla Sep 24 '16 at 09:06
  • did you ever solve this? facing the same issue – behelit Mar 26 '19 at 06:07

3 Answers3

0

Try this:

public String getContactNameByNumber(String number) {
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    String name = "?";

    ContentResolver contentResolver = getContentResolver();
    Cursor contact = contentResolver.query(uri, new String[] {BaseColumns._ID,
            ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);

    try {
        if (contact != null && contact.getCount() > 0) {
            contact.moveToNext();
            name = contact.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
        }
    } finally {
        if (contact != null) {
            contact.close();
        }
    }

    return name;
}
Sufian
  • 6,405
  • 16
  • 66
  • 120
Rakesh Rangani
  • 1,039
  • 10
  • 13
  • Does this return me a contact that has phone number "123 456 7890", when I am trying to search with "1234"? I guess, this works when we enter complete phone number. – User12111111 Sep 08 '14 at 10:56
  • yes, it's works when we enter complete phone number. – Rakesh Rangani Sep 08 '14 at 11:04
  • 1
    This is an integral copy of http://stackoverflow.com/questions/3712112/search-contact-by-phone-number with no reference. Good job! – luiscosta Jan 11 '17 at 11:49
-1

in you code to change

constraint.toString() instead of write constain.replaceAll(" ", "");

that remove all space from string

  • 2
    To make it clear, I am trying to search contacts which starts with "1234", the resulting should return me contacts with phone number "1234567890" and "123 4567 890". Some of the Android devices saves contact with space. How can I retrieve both.? – User12111111 Sep 08 '14 at 13:12
-1

try this:

String whereName = ContactsContract.Data.MIMETYPE + " = ?";
String number;
String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE };
Cursor nameCur = con.query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.Phone.NUMBER);


while (nameCur.moveToNext()) {
    number = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

    if (number != null) {
        if (number.contains(num)) {
            System.out.println(number);

        }
    }

}
nameCur.close();

In up comment of answer said that: "I am trying to search contacts which starts with "1234", the resulting should return me contacts with phone number "1234567890" and "123 4567 890"."

My code solve this problem.

jack
  • 27
  • 3