仅从Android联系人中获取电子邮件联系人

时间:2013-06-12 02:32:46

标签: android contacts

我正在尝试仅提取联系人中可用的电子邮件联系人。现在我有一个显示所有联系人的解决方案,如果选择的联系人没有电子邮件地址,它会吐口水,说明找不到电子邮件地址。相反,我想显示只有电子邮件地址的联系人。

以下是我尝试的查询:

Cursor cursor = null;  
            String emailid = "";
            List<String> allids = new ArrayList<String>();
            int emailIds = 0;
            try 
            {  

                Uri result = data.getData();  
                String id = result.getLastPathSegment();  
                Log.e("Email","TRY"+emailid);
                cursor = getContentResolver().query(Email.CONTENT_URI, null, Email.CONTACT_ID + "=?", new String[] { id }, null);                   
                /*cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
                new String[]{id}, null);*/
                emailIds = cursor.getColumnIndex(Email.DATA);
                if (cursor.moveToFirst()) 
                {
                    while (cursor.isAfterLast() == false)
                    {
                        emailid = cursor.getString(emailIdx);
                        allids.add(emailid);
                        cursor.moveToNext();
                    }
                } 

                else 
                {
                    //no results actions

                }  
            }

有人可以告诉我如何让电子邮件查询部分正常工作吗?

谢谢!

1 个答案:

答案 0 :(得分:5)

2获取与电子邮件联系的算法

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。 但是很慢,这是另一种算法,速度更快:

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

第一段代码花了大约4秒的时间在我的测试设备上获取联系人,第二段代码花了大约0.04秒。