从电子邮件ID的地址簿中获取联系人姓名

时间:2014-03-31 09:39:56

标签: java android

如果我有电子邮件ID,我需要从地址簿中获取相应的名称,是否可以这样做?

2 个答案:

答案 0 :(得分:1)

使用电子邮件匹配特定URI

,根据emailID获取联系人
Uri uri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Email.CONTENT_FILTER_URI, Uri.encode(emailIdString));

其中emailIdString是您要为其提取联系人的电子邮件地址。

Cursor contactLookup = cr.query(uri, new String[] {ContactsContract.CommonDataKinds.Email.CONTACT_ID, ContactsContract.Data.DISPLAY_NAME }, null, null, null);

contactLookup游标将包含其emailID与emailIdString匹配的所有联系人。

答案 1 :(得分:0)

这可能适合你:

    public String getCallerFromEmailId(String emailId) {
    // TODO Auto-generated method stub
    String callerName = null;
    ContentResolver resolver = context.getContentResolver();
    Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);

    if (cursor.getCount() > 0) {
        while (cursor.moveToNext()) {

            Cursor pcursor = resolver.query(
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                    null, ContactsContract.CommonDataKinds.Email.DATA
                            + " = ?", new String[] { emailId }, null);

            while (pcursor.moveToNext()) {
                callerName = pcursor
                        .getString(pcursor
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                return callerName;
            }
            pcursor.close();

        }
    }
    cursor.close();
    return callerName;
}