62

我只想显示那些存在电子邮件地址的联系人姓名。否则,该联系人姓名不应显示在列表中。我怎样才能做到这一点?有人可以帮我吗?

4

7 回答 7

123

这是我提取电子邮件地址的超快速查询。它比其他答案建议的拉所有联系人列要快得多......

public ArrayList<String> getNameEmailDetails() {
    ArrayList<String> emlRecs = new ArrayList<String>();
    HashSet<String> emlRecsHS = new HashSet<String>();
    Context context = getActivity();
    ContentResolver cr = context.getContentResolver();
    String[] PROJECTION = new String[] { ContactsContract.RawContacts._ID, 
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.PHOTO_ID,
            ContactsContract.CommonDataKinds.Email.DATA, 
            ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
    String order = "CASE WHEN " 
            + ContactsContract.Contacts.DISPLAY_NAME 
            + " NOT LIKE '%@%' THEN 1 ELSE 2 END, " 
            + ContactsContract.Contacts.DISPLAY_NAME 
            + ", " 
            + ContactsContract.CommonDataKinds.Email.DATA
            + " COLLATE NOCASE";
    String filter = ContactsContract.CommonDataKinds.Email.DATA + " NOT LIKE ''";
    Cursor cur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION, filter, null, order);
    if (cur.moveToFirst()) {
        do {
            // names comes in hand sometimes
            String name = cur.getString(1);
            String emlAddr = cur.getString(3);

            // keep unique only
            if (emlRecsHS.add(emlAddr.toLowerCase())) {
                emlRecs.add(emlAddr);
            }
        } while (cur.moveToNext());
    }

    cur.close();
    return emlRecs;
}

我尝试了“Agarwal Shankar”提供的代码,但在我的测试设备上获取联系人需要大约 4 秒,而此代码大约需要 0.04 秒。

于 2013-04-05T04:32:54.753 回答
78
public ArrayList<String> getNameEmailDetails(){
        ArrayList<String> names = new ArrayList<String>();
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                Cursor cur1 = cr.query( 
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
                                new String[]{id}, 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);
                    if(email!=null){
                        names.add(name);
                    }
                } 
                cur1.close();
            }
        }
        return names;
    }

上述方法返回具有电子邮件 ID 的名称数组列表。

于 2012-04-12T05:14:36.523 回答
3
if (cur.getCount() > 0) {
          while (cur.moveToNext()) {
              String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
              String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

              Cursor emailCur = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id},null); 
              while (emailCur.moveToNext()) { 
                      String email = emailCur.getString( emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                      Log.e("Email",name+" "+email);
                  } 
                  emailCur.close();
              }

          }
于 2012-04-12T06:51:14.550 回答
2

另一种解决方案。

private static final Uri URI_CONTACT_DATA = ContactsContract.Data.CONTENT_URI;
private static final String COLUMN_EMAIL = ContactsContract.CommonDataKinds.Email.ADDRESS;
private static final String COLUMN_DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME_PRIMARY;
private static final String COLUMN_MIMETYPE = ContactsContract.Data.MIMETYPE;

private static final String[] PROJECTION = {
        COLUMN_DISPLAY_NAME,
        COLUMN_EMAIL,
        COLUMN_MIMETYPE
};

private Cursor getCursor() {
    ContentResolver resolver = context.getContentResolver();
    String selection = COLUMN_MIMETYPE + "=?";
    final String[] selectionArgs = {ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE};
    return resolver.query(URI_CONTACT_DATA, PROJECTION, selection, selectionArgs, null);
}

问题是表在ContactsContract.Contacts.CONTENT_URI保存整个联系人数据库。这包括电话号码、电子邮件、组织,甚至是完全自定义的数据,因此如果不使用ContactsContract.Data.MIMETYPE. 此数据库中的一行包含一个与某个帐户相关的值(或多个值,它有 15 个通用列),因此您可能需要自己对它们进行分组。我需要这个来自动完成电子邮件,所以格式(每行的电子邮件)是完美的。

于 2016-03-18T09:24:20.077 回答
0

如果您已经可以获取联系人及其电子邮件地址(如果存在),为什么不从您的列表中删除没有电子邮件地址的联系人?

有关 Android 联系人 API 的更多信息,请参阅此处

于 2012-04-12T03:39:24.043 回答
0

这是从联系人列表中获取联系人电子邮件 ID 的简单方法。您需要在以下方法中传递用户的联系人 ID,如果存在,它将返回您的电子邮件 ID

 public String getEmail(String contactId) {
    String emailStr = "";
    final String[] projection = new String[]{ContactsContract.CommonDataKinds.Email.DATA,
            ContactsContract.CommonDataKinds.Email.TYPE};

    Cursor emailq = managedQuery(ContactsContract.CommonDataKinds.Email.CONTENT_URI, projection, ContactsContract.Data.CONTACT_ID + "=?", new String[]{contactId}, null);

    if (emailq.moveToFirst()) {
        final int contactEmailColumnIndex = emailq.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);

        while (!emailq.isAfterLast()) {
            emailStr = emailStr + emailq.getString(contactEmailColumnIndex) + ";";
            emailq.moveToNext();
        }
    }
    return emailStr;
}

此外,如果您想了解如何在您的应用程序中获取联系人列表,请点击此链接:在应用程序 android - trinitytuts 中显示联系人列表

于 2016-01-22T05:33:34.863 回答
0

声明一个全局变量

// Hash Maps
Map<String, String> nameEmailMap = new HashMap<String, String>();

然后使用下面的函数

private void getEmailIDs() {

    Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, null, null, null);

    // Loop Through All The Emails
    while (emails.moveToNext()) {

        String name = emails.getString(emails.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        String email = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));

        // Enter Into Hash Map
        nameEmailMap.put(email, name);

    }

    // Get The Contents of Hash Map in Log
    for (Map.Entry<String, String> entry : nameEmailMap.entrySet()) {
        String key = entry.getKey();
        Log.d(TAG, "Email :" + key);
        String value = entry.getValue();
        Log.d(TAG, "Name :" + value);
    }
    emails.close();
}

请记住,在上面的示例中,键是电子邮件,值是名称,因此请阅读您的内容,例如 mahaXXXX@gmail.com->Mahatma Gandhi 而不是 Mahatma Gandhi->mahaXXXX@gmail.com

于 2019-01-17T00:13:37.540 回答