0

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.

Community
  • 1
  • 1
Arun Kumar
  • 2,874
  • 1
  • 18
  • 15

1 Answers1

0

Use the following code:

String [] projection = { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME };
Cursor cursor =
    this.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
                                    projection, null, null, null);
if (cursor.moveToNext()) {
    int displayNameIdx = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
    String displayName = 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 + " = ?", 
                new String[]{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();
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
  • but i have already phone no like 5551111000 – Arun Kumar Dec 27 '11 at 16:45
  • Yes, as I mentioned in the edit of my answer: you need to iterate all the contacts and check all their associated phone numbers. The problem is that the information in Android is organized based on contact ids not on phone numbers. Just keep in mind that it is even possible to have several contacts with the same phone number. – Boris Strandjev Dec 27 '11 at 16:47
  • but i have other solution to display name from phone no – Arun Kumar Dec 27 '11 at 17:07
  • http://stackoverflow.com/questions/2174048/how-to-look-up-a-contacts-name-from-their-phone-number-on-android – Arun Kumar Dec 27 '11 at 17:08
  • Actually this should be right: as long as you can treat the ContentProviders as ordinary database cursors you can select by whatever where clause you wish. Then all it takes is to get the phone details and from them fetch the contact_id. Then you can get to select either the display name or all the other data available through StructuredName. I am going to edit my post so that it fits the question better, but I agree you contributed to this update. – Boris Strandjev Dec 27 '11 at 17:14