Last active
June 15, 2019 21:30
-
-
Save balachandarlinks/87683cafd64320213c17 to your computer and use it in GitHub Desktop.
Retrieve contact name by giving a phone number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* @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