Android: get contact name from phone number

I would like to get the name of the contact associated with the incoming phone number. When I process the incoming number in the broascastreceiver command with a string with the name of the incoming caller, this will help my project a lot.

I would think that this is due to a query using the sql WHERE clause as a filter, but do I need to sort the contacts? An example or tip will be very helpful.

+44
android phone-number contactscontract android-contacts
Jun 20 '10 at 13:23
source share
5 answers

To do this, you need to use the optimized PhoneLookup provider as described.

Add AndroidManifest.xml permission:

 <uses-permission android:name="android.permission.READ_CONTACTS"/> 

Then:

 public String getContactName(final String phoneNumber, Context context) { Uri uri=Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,Uri.encode(phoneNumber)); String[] projection = new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}; String contactName=""; Cursor cursor=context.getContentResolver().query(uri,projection,null,null,null); if (cursor != null) { if(cursor.moveToFirst()) { contactName=cursor.getString(0); } cursor.close(); } return contactName; } 
+59
Jun 20 '10 at 13:47
source share
— -

Although this has already been answered, but here is the complete function to get the name of a contact from a number. Hope this helps others:

 public static String getContactName(Context context, String phoneNumber) { ContentResolver cr = context.getContentResolver(); Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); Cursor cursor = cr.query(uri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null); if (cursor == null) { return null; } String contactName = null; if(cursor.moveToFirst()) { contactName = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME)); } if(cursor != null && !cursor.isClosed()) { cursor.close(); } return contactName; } 

[ Update based on Marcus comment]

You will need to request this permission:

<uses-permission android:name="android.permission.READ_CONTACTS"/>

+81
Aug 05 '13 at 18:12
source share

This was very useful, here is my last code for retrieving the caller name, id and photo:

 private void uploadContactPhoto(Context context, String number) { Log.v("ffnet", "Started uploadcontactphoto..."); String name = null; String contactId = null; InputStream input = null; // define the columns I want the query to return String[] projection = new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID}; // encode the phone number and build the filter URI Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); // query time Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null); if (cursor.moveToFirst()) { // Get values from contacts database: contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup._ID)); name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)); // Get photo of contactId as input stream: Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(contactId)); input = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), uri); Log.v("ffnet", "Started uploadcontactphoto: Contact Found @ " + number); Log.v("ffnet", "Started uploadcontactphoto: Contact name = " + name); Log.v("ffnet", "Started uploadcontactphoto: Contact id = " + contactId); } else { Log.v("ffnet", "Started uploadcontactphoto: Contact Not Found @ " + number); return; // contact not found } // Only continue if we found a valid contact photo: if (input == null) { Log.v("ffnet", "Started uploadcontactphoto: No photo found, id = " + contactId + " name = " + name); return; // no photo } else { this.type = contactId; Log.v("ffnet", "Started uploadcontactphoto: Photo found, id = " + contactId + " name = " + name); } 

... then just do whatever you want with the help of “input” (their photo is like InputStream), “name” and “contactId”.

And here are the documents listing 15 or so columns that you have access to, just add them to the projection at the beginning of the code above: http://developer.android.com/reference/android/provider/ContactsContract.PhoneLookup.html

+24
Jan 01 2018-11-21T00:
source share

This version matches Vikram.exe answer with code to avoid ANR

 interface GetContactNameListener { void contactName(String name); } public void getContactName(final String phoneNumber,final GetContactNameListener listener) { new Thread(new Runnable() { @Override public void run() { ContentResolver cr = getContentResolver(); Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null); if (cursor == null) { return; } String contactName = null; if(cursor.moveToFirst()) { contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)); } if(cursor != null && !cursor.isClosed()) { cursor.close(); } listener.contactName(contactName); } }).start(); } 
+2
Oct 05 '15 at 20:27
source share

Transfer the contact number from which you receive the call in the following way. This method checks if the contact is saved on your mobile phone or not. If the contact is saved, it will return the contact name, otherwise it will return an unknown number string

Add this code to your broadcast receiver class

  public String getContactDisplayNameByNumber(String number,Context context) { Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); name = "Incoming call from"; ContentResolver contentResolver = context.getContentResolver(); Cursor contactLookup = contentResolver.query(uri, null, null, null, null); try { if (contactLookup != null && contactLookup.getCount() > 0) { contactLookup.moveToNext(); name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)); // this.id = // contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.CONTACT_ID)); // String contactId = // contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID)); }else{ name = "Unknown number"; } } finally { if (contactLookup != null) { contactLookup.close(); } } return name; } 

to get the source code to visit this link

+1
Dec 18 '16 at 14:12
source share



All Articles