Android: Search for contacts based on phone number

I need to find a contact by phone number. Here is the code that works for extracting contacts. The Android API level I'm 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 can get a contact that does not have a place with a phone number stored in the contact table. If the phone number has a space between them, the request above is not executed. For example, for a contact, if the phone number is saved as 1234567890, and when I search with a value of 1234, this contact will be restored. But it fails if the contact is saved as "123 456 7890."

In the bottom line, when I try to find contacts containing or containing "1234" with a phone number, as a result, we must return me contacts with a phone number of "1234567890" and "123 4567 890". Since some of the android phones store phone numbers with a gap between them.

How can i solve this. Any help is appreciated.

+6
source share
3 answers

Try the following:

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

in your code to change

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

which remove all space from the string

-1
source

try the following:

 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(); 

The commentary to the answer says that: "I am trying to find contacts that begin with" 1234 ", as a result we must return the contacts with the phone numbers" 1234567890 "and" 123 4567 890 ".

My code solves this problem.

-1
source

Source: https://habr.com/ru/post/974969/


All Articles