Get Contact Name from Mobile Number in Android

A Simple Function to get the contact Name from Mobile Number in Android


public String retrieveContactRecord(Context context, String phoneNo) {
try {
Uri uri = Uri.withAppendedPath(
ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(phoneNo));
String[] projection = new String[] {
ContactsContract.PhoneLookup._ID,
ContactsContract.PhoneLookup.DISPLAY_NAME };
String selection = null;
String[] selectionArgs = null;
String sortOrder = ContactsContract.PhoneLookup.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
ContentResolver cr = context.getContentResolver();
if (cr != null) {
Cursor resultCur = cr.query(uri, projection, selection,
selectionArgs, sortOrder);
if (resultCur != null) {
while (resultCur.moveToNext()) {
contactId = resultCur
.getString(resultCur
.getColumnIndex(ContactsContract.PhoneLookup._ID));
contactName = resultCur
.getString(resultCur
.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
Log.e("Info", "Contact Id : " + contactId);
Log.e("Info", "Contact Display Name : " + contactName);

break;
}
resultCur.close();

}
}
} catch (Exception sfg) {
Log.e("Error", "Error in loadContactRecord : " + sfg.toString());
return phoneNo;
}

return contactName;
}


 AndroidManifest.xml
==================

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

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();

Armed Posts

Recent