仅从联系人列表Android获取电子邮件地址

时间:2012-04-12 03:35:55

标签: android

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

7 个答案:

答案 0 :(得分:118)

这是我提取电子邮件地址的超快查询。如其他答案所示,它比拉动所有接触柱要快得多......

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秒。

答案 1 :(得分:70)

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的名称的arraylist。

答案 2 :(得分: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个通用列),因此您可能需要自己对它们进行分组。我需要这个来自动填充电子邮件,所以格式(每行电子邮件)是完美的。

答案 3 :(得分:1)

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();
              }

          }

答案 4 :(得分:0)

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

有关Android Contacts API的详情,请参阅here

答案 5 :(得分:0)

声明全局变量

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();
}

然后使用下面的功能

var array = str.Split(new []{Environment.NewLine},StringSplitOptions.RemoveEmptyEntries);
var regex = new Regex(@"(Dimension)\s+(text)\s+(=)\s+(?<Value>([+-]?(\d*\.)?\d+)+)",RegexOptions.Compiled);
var resultArray =  from entry in array
                let match = regex.Match(entry)
                where match.Success                
                select decimal.Parse(match.Groups["Value"].Value);

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

答案 6 :(得分:-1)

以下是从联系人列表中获取联系人电子邮件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;
}

如果您想了解如何在您的应用中获取联系人列表,请点击以下链接:show contact list in app android - trinitytuts