Skip to content Skip to sidebar Skip to footer

How To Get Nick Name From Phone Number In Android

Possible Duplicate: How to look-up a contact's name from their phone number on Android? How to get nick name from phone number in android.What are content provider used.

Solution 1:

Use the following code:

String [] projection = { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME };
Cursorcursor=this.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
                                    projection, null, null, null);
if (cursor.moveToNext()) {
    intdisplayNameIdx= cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
    StringdisplayName= cursor.getString(displayNameIdx);
}

I am not entirely sure you need the display name, but you can get further details if you get to use ContactsContract.CommonDataKinds.StructuredName:

String [] projection = { ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
                         ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME };
String where = ContactsContract.Data.CONTACT_ID
               + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
String[] whereParameters =
    { contactId, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
Cursor cursor =
        this.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
                projection, where, whereParameters, null);

EDIT: Ah and now as I see you want to map the nickname to a phone number. This is how you select the contact_id out of given phone number:

String [] projection =
    { ContactsContract.CommonDataKinds.Phone.CONTACT_ID };
Cursor phoneCursor =
        this.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                projection, ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?", 
                newString[]{number}, null);
int idIndex = phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);
while (phoneCursor.moveToNext()) { 
    String id = phoneCursor.getString(numberIndex);
    Log.i(LOG_TAG, "This is the contact id associatated with the given number" + id);
} 
phoneCursor.close();

Post a Comment for "How To Get Nick Name From Phone Number In Android"