简体   繁体   中英

Android: Search Contact based on Phone Number

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.

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

in you code to change

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

that remove all space from string

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM