如何从联系人那里获取电子邮件

Posted

技术标签:

【中文标题】如何从联系人那里获取电子邮件【英文标题】:How to get email from contact 【发布时间】:2019-03-06 14:20:27 【问题描述】:

此代码可以正常获取姓名和电话,但我不知道如何从此代码获取电子邮件。这是我的代码:

public static ArrayList<ContentValues> getContactDetails(final Context mContext)
ArrayList<ContentValues> contactList = new ArrayList<ContentValues>();


String order = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC";
Cursor managedCursor = mContext.getContentResolver().query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
        null, order);

int _number = managedCursor
        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int _name = managedCursor
        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int _id = managedCursor
        .getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID);



while (managedCursor.moveToNext()) 

    ContentValues values = new ContentValues();
    Contact mContact = new Contact();


        values.put(ContactClass.CONTACT_NAME, managedCursor.getString(_name));
        values.put(ContactClass.CONTACT_MOBILE_NUMBER, managedCursor.getString(_number).replaceAll("\\s+",""));
        mContact.setPhNo(managedCursor.getString(_number).replaceAll("\\s+",""));
        mContact.setName(managedCursor.getString(_name));

        contactList.add(values);
        serverContactList.add(mContact);

    


return contactList;

我想在这里获取电子邮件并添加到 serverContactList 列表中。

【问题讨论】:

【参考方案1】:

我编辑了您的代码,将您的其余代码添加到 2 个待办事项中。

public static ArrayList<ContentValues> getContactDetails(final Context mContext)  
    // todo rest of things

    int _id = managedCursor
            .getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID);

    while (managedCursor.moveToNext()) 
        // we will get emails for a contact id
        String id = managedCursor.getString(_id);
        Cursor cur1 = mContext.getContentResolver().query(
                ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                new String[]id, null);
        if (cur1 != null) 
            while (cur1.moveToNext()) 
                //to get the contact names
                String name = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                Log.e("Name :", name);
                String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                Log.e("Email", email);
            
        
        cur1.close();

        // todo rest of things
    
    return contactList;

【讨论】:

【参考方案2】:
    永远不要使用managedCursor。 查询 Data.CONTENT_URI 而不是 Phone.CONTENT_URI 以访问所有数据项(包括电话和电子邮件以及其他如果需要) 将 MIMETYPE 查询限制为您需要的项目

Map<Long, Contact> contacts = new HashMap<>();

String[] projection = Data.CONTACT_ID, Data.DISPLAY_NAME, Data.MIMETYPE, Data.DATA1, Data.DATA2, Data.DATA3;

// query only emails/phones/events
String selection = Data.MIMETYPE + " IN ('" + Phone.CONTENT_ITEM_TYPE + "', '" + Email.CONTENT_ITEM_TYPE + "')";
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(Data.CONTENT_URI, projection, selection, null, null);

while (cur != null && cur.moveToNext()) 
    long id = cur.getLong(0);
    String name = cur.getString(1); // full name
    String mime = cur.getString(2); // type of data (phone / email)
    String data = cur.getString(3); // the actual info, e.g. +1-212-555-1234

    Log.d(TAG, "got " + id + ", " + name + " - " + data);

    // add info to existing list if this contact-id was already found, or create a new list in case it's new
    Contact contact;
    if (contacts.containsKey(id)) 
        contact = contacts.get(id);
     else 
        contact = new Contact();
        contacts.put(id, contact);
        contact.setName(name);
    
    if (mime == Phone.CONTENT_ITEM_TYPE) 
        contact.setPhNo(data);
     else 
        contact.setEmail(data);
    

【讨论】:

以上是关于如何从联系人那里获取电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

如何从联系人列表中获取电子邮件地址?

如何从联系人中获取电子邮件和姓氏?

如何使用 ldap 和 spring security 从登录的用户那里获取电子邮件

如何从联系人列表中获取电子邮件 ID、姓名和号码

如何将选定的电子邮件从 android 联系人获取到应用程序? [复制]

如何从android中的单个查询中获取联系人的名字,姓氏,电子邮件ID