简体   繁体   English

如何从Android中的联系人读取电子邮件字段?

[英]How to read email field from contact in android?

In my app I read all the contacts from my phone into list view. 在我的应用程序中,我将手机中的所有联系人读入列表视图。 After clicking on a particular view from the list, I want to read the name, phone and e-mail address. 从列表中单击特定视图后,我想阅读名称,电话和电子邮件地址。 I manage to read the name and phone without a problem. 我设法毫无问题地读取姓名和电话。 But I can not get the email. 但是我收不到电子邮件。

Say I have 2 contacts: 说我有2位联络人:

Bill
55-555-555
bil@example.com

Mark
66-666-666
mark@example.com

So the field of the email is called reverse. 因此,电子邮件的字段称为反向。 For Bill I get Mark's email And for Mark I get Bill's email. 对于比尔,我会收到马克的电子邮件;对于马克,我会收到比尔的电子邮件。 Other values ​​are correct. 其他值是正确的。

private  HashMap<String,String> getContactNames() 
{
        HashMap<String,String>contact=new HashMap<>();
        ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        cursor.moveToPosition(pos_listView);
        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        phones.moveToPosition(pos_listView);
        String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        Cursor emailcur = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, null,null, null);
        emailcur.moveToPosition(pos_listView);
        String email = emailcur.getString(emailcur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA1));

        contact.put("name",name);
        contact.put("phoneNumber",phoneNumber);
        contact.put("email",email);

        cursor.close();
        return contact;
    }

Nope, that's definitely not the way it works. 不,那绝对不是它的工作方式。

There are 3 different tables in ContactsContract APIs: ContactsContract API中有3个不同的表:

  1. Contacts 联络人
  2. RawContacts 原始联系人
  3. Data (which you can access either directly via Data.CONTENT_URI , or through helper sub-tables via Phone.CONTENT_URI , Email.CONTENT_URI , etc.) 数据 (您可以通过Data.CONTENT_URI直接访问,也可以通过Phone.CONTENT_URIEmail.CONTENT_URI等通过帮助子表Phone.CONTENT_URI

The position of a contact within the list of contacts has nothing to do with the position if its phone or emails within their tables, so moveToPosition(pos_listView) is very wrong. 如果联系人的电话或电子邮件在表中,则该联系人在列表中的位置与该位置无关,因此moveToPosition(pos_listView)是非常错误的。

This is what you should do: 这是您应该做的:

  1. When reading the list of all contacts, keep track of their contact-id's via Contacts._ID and make sure you can get it when a contact is clicked 阅读所有联系人的列表时,请通过Contacts._ID跟踪其联系人ID,并确保单击联系人时可以获取它。
  2. When a contact is clicked, get its contact-id, and send it to your method. 单击联系人后,获取其联系人ID,并将其发送给您的方法。
  3. In your contact info method, query only once for all the phones and emails (and possibly more info if you need) to get them all at once. 在您的联系信息方法中,仅查询一次所有电话和电子邮件(如果需要,还可以查询更多信息)以一次全部获取。

Your method would then look something like this: 您的方法将如下所示:

private void getContactInfo(long contactId) {
    String[] projection = {Data.DISPLAY_NAME, Data.MIMETYPE, Data.DATA1};
    String selection = Data.CONTACT_ID + "=" + contactId + " AND " + Data.MIMETYPE + " IN ('" + Phone.CONTENT_ITEM_TYPE + "', '" + Email.CONTENT_ITEM_TYPE + "')";
    Cursor cur = cr.query(Data.CONTENT_URI, projection, selection, null, null);

    Log.i(TAG, "dumping info for contact id: " + contactId);
    while (cur.moveToNext()) {
        String name = cur.getString(0);
        String mime = cur.getString(1); // type of data: email, phone, company, etc.
        String data = cur.getString(2); // the actual info, e.g. +1-212-555-1234

        String type = Phone.CONTENT_ITEM_TYPE.equals(mime) ? "phone" : "email";

        Log.i(TAG, "info: " + name + " - " + type + ": " + data);
    }
    cur.close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM