16

I am trying to get the sender's name from the contacts database using a content provider.

The problem is I don't know how to implement it. Like now I can only pull the phone number from the smsMessage. I need to check to see if the phone number that is calling is in the users contacts first and if it is display the name if it is not then display the number.

Christopher Orr
  • 110,418
  • 27
  • 198
  • 193
ronnie173
  • 207
  • 1
  • 3
  • 10
  • 3
    You can probably generalize this question to something like 'How do you look up a contact given a raw, unformatted phone number string?' – Roman Nurik Feb 01 '10 at 08:47

2 Answers2

25

Yes, this is possible using ContactsContract.PhoneLookup.CONTENT_FILTER_URI in Android 2.0 and higher and Contacts.Phones.CONTENT_FILTER_URL in Android 1.6 and earlier.

For example usage, see the documentation for ContactsContract.PhoneLookup. Excerpt below:

// Android 1.6 and earlier (backwards compatible for Android 2.0+)
Uri uri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Uri.encode(phoneNumber));

// Android 2.0 and later
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));

// Query the filter URI
String[] projection = new String[]{ PhoneLookup.DISPLAY_NAME, ...
Cursor cursor = context.getContentResolver().query(uri, projection, ...

UPDATE: The format of the phone number does not matter. Comparison is robust and highly optimized on Android; it's done using a native sqlite function named PHONE_NUMBERS_EQUAL. For more details, search the codebase for this method. By the way, I'm not certain if it's safe to use that function directly in your own apps, but I wouldn't.

Roman Nurik
  • 29,665
  • 7
  • 84
  • 82
  • 2
    What specifically goes into the rest of that query? This answer isn't much more helpful than the already existing documentation. – eternalmatt Mar 19 '11 at 21:23
  • 4
    Does the format of the phone number matter? That is, will `(555) 555-5555` and `555-555-5555` both match `555555555`? – Matt Briançon Apr 18 '11 at 19:46
  • Does the format of the phone number matter? That is, will (555) 555-5555 and 555-555-5555 both match 555555555? [2] – Felipe Oct 05 '11 at 01:59
  • The format of the numbers does not matter. Updating my answer now. – Roman Nurik Oct 05 '11 at 22:11
  • 3
    will absence of country code in a phone number create any problem in lookup ? I mean if a stored contact contains country code as a prefix and i am trying to match it with a number without country code, will it match with that? – Purush Pawar May 04 '13 at 10:09
  • @PuruPawar did you get the answer of Country code ? I am facing some similar situation of searched string not containing country code while number in the database has country code. – ShReYaNsH Nov 10 '16 at 12:47
20

Here's what I did

ContentResolver localContentResolver = this.mContext.getContentResolver();
Cursor contactLookupCursor =  
   localContentResolver.query(
            Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, 
            Uri.encode(phoneNumber)), 
            new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID}, 
            null, 
            null, 
            null);
try {
while(contactLookupCursor.moveToNext()){
    String contactName = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
    String contactId = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup._ID));
    Log.d(LOGTAG, "contactMatch name: " + contactName);
    Log.d(LOGTAG, "contactMatch id: " + contactId);
    }
} finally {
contactLookupCursor.close();
} 

Passing in your phone number you already have into Uri.encode(phoneNumber)

Intrications
  • 16,782
  • 9
  • 50
  • 50
Andrew
  • 7,548
  • 7
  • 50
  • 72
  • More good examples here: http://stackoverflow.com/questions/3079365/android-retrieve-contact-name-from-phone-number/8695649 – mdaddy Jan 01 '12 at 21:59
  • This helped me a lot. Thanks Andrew, Just one little question. Is there a possibility to change this code a bit more, and making it search/match a phone number to contact name as you type? Let's say my number is 444 886 444. I happen to remember '886', not the rest. It should search and match and 'Give' me some results. If you're willing to answer my comment, It'd be much appreciated. If Anyone has a link for this kind of thing, please post. Thanks in advance! – Paramone Dec 24 '13 at 11:22