我如何在5秒内获得所有联系人,包括原始联系人

时间:2015-12-28 08:08:02

标签: android

我想获取所有联系人数据,包括所有原始联系人。 我提到了link

这是我正在使用的代码:

Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,    Long.valueOf(raw_contact_id));
Uri entityUri = Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);

Cursor c = getContentResolver().query(entityUri,
        new String[] { RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1,
                Entity.DATA2, Entity.DATA3, Entity.DATA4, Entity.DATA5, Entity.DATA6, Entity.DATA7,
                Entity.DATA8, Entity.DATA9, Entity.DATA10, Entity.DATA11, Entity.DATA12, Entity.DATA13,
                Entity.DATA14/* , Entity.DATA15 */ },
        null, null, null);

try {
    while (c.moveToNext()) {
        String sourceId = c.getString(0);
        if (!c.isNull(1)) {
            String mimeType = c.getString(2);
            // if (!mimeType.contains("photo")) {
            String data1 = c.getString(3);
            String data2 = c.getString(4);
            String data3 = c.getString(5);
            String data4 = c.getString(6);
        }
    }
} finally {
    c.close();
}

问题是此代码的实现需要花费大量时间来获取所有联系人数据。

还有其他方法可以在5秒内获得联系人数据。

1 个答案:

答案 0 :(得分:1)

这就是我得到这个名字的原因。电子邮件地址,您也可以检索其他数据

ArrayList<String> emailAddressList = new ArrayList<String>();
ArrayList<String> contactNameList = new ArrayList<String>();

HashSet<String> emlRecsHS = new HashSet<String>();

ContentResolver cr = getContentResolver();
String[] PROJECTION = new String[] {ContactsContract.CommonDataKinds.Email.DATA, ContactsContract.Contacts.DISPLAY_NAME};

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 emlAddress = cur.getString(0);
        String contactName = cur.getString(1);

        // keep unique only
        if (emlRecsHS.add(emlAddress.toLowerCase(Locale.US)))
        {
            emailAddressList.add(emlAddress);
            contactNameList.add(contactName);
        }
    } while (cur.moveToNext());
}

cur.close();