Skip to content

Instantly share code, notes, and snippets.

@balachandarlinks
Last active June 15, 2019 21:30
Show Gist options
  • Save balachandarlinks/87683cafd64320213c17 to your computer and use it in GitHub Desktop.
Save balachandarlinks/87683cafd64320213c17 to your computer and use it in GitHub Desktop.
Retrieve contact name by giving a phone number
/**
*
* @param context
* @param phoneNumber
* @return contactName
*/
public static String getContactName(Context context, String phoneNumber) {
Uri uri = Uri.withAppendedPath(
PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(phoneNumber));
Cursor cursor = context.getContentResolver().query(
uri,
new String[] { PhoneLookup.DISPLAY_NAME },
null,
null,
null);
String contactName = phoneNumber;
if (cursor != null) {
if (cursor.moveToFirst()) {
contactName = cursor.getString(cursor
.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
cursor.close();
}
return contactName;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment