Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search contact by phone number

In my app, user writes a phone number, and I want to find the contact name with that phone number?

I usually search the contacts like this:

Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,             null, null, null, null); 

But I do this to access all contacts... In this app I only want to get the contact name of the given phone number... How can I restrict the query?

Or do I have to go trough all contacts and see if any has the given phone number? But I believe that this can be very slow this way...

like image 769
Tiago Costa Avatar asked Sep 14 '10 19:09

Tiago Costa


People also ask

Is there a way to search for someone by phone number?

The most frequently used worldwide mobile application to search for someone using their phone number is Truecaller. It gives you free access to search for mobile numbers worldwide. The platform will retrieve the name and the location of the phone number where it is registered.

Is there a free site to look up a phone number?

One site that dependably performs a free and trustworthy reverse phone lookup is the simply named Phone Lookup. To use the site, simply enter the full 10-digit phone number you want to perform a reverse search on and click Search.


1 Answers

If you want the complete code:

public String getContactDisplayNameByNumber(String number) {     Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));     String name = "?";      ContentResolver contentResolver = getContentResolver();     Cursor contactLookup = contentResolver.query(uri, new String[] {BaseColumns._ID,             ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);      try {         if (contactLookup != null && contactLookup.getCount() > 0) {             contactLookup.moveToNext();             name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));             //String contactId = contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID));         }     } finally {         if (contactLookup != null) {             contactLookup.close();         }     }      return name; } 
like image 73
Felipe Avatar answered Oct 03 '22 03:10

Felipe