Android联系人提供商仅获取所有电子邮件的电话联系人

时间:2013-03-28 15:40:28

标签: android performance android-contentprovider android-contacts android-contentresolver

我需要获取所有电话联系人及其电子邮件地址和照片uri:

这就是我在做什么:

private void getContacts() {

        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(Contacts.CONTENT_URI, null, null, null, Contacts.DISPLAY_NAME);

        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {

                // if
                // (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))
                // > 0) {

                Contact contact = new Contact();

                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));

                Uri uri = getContactPhotoUri(Long.parseLong(id));
                // set photoUri
                contact.setContactPhotoUri(uri);

                // set name
                contact.setContactName(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));

                // get the phone number
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
                while (pCur.moveToNext()) {

                    // set phone munber
                    contact.setContactNumber(pCur.getString(pCur
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                    contacts.add(contact);

                }
                pCur.close();

                // get email and type
                Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[] { id }, null);
                while (emailCur.moveToNext()) {
                    // This would allow you get several email addresses
                    // if the email addresses were stored in an array

                    // set email
                    contact.setContactEmail(emailCur.getString(emailCur
                            .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));

                    contacts.add(contact);

                }
                emailCur.close();

            }
        }

        cur.close();
        contactAdapter = new ContactAdapter(this, R.id.contactList, contacts);

        // }

    }

    public Uri getContactPhotoUri(long contactId) {
        Uri photoUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
        photoUri = Uri.withAppendedPath(photoUri, Contacts.Photo.CONTENT_DIRECTORY);
        return photoUri;
    }

我的问题是获取所有联系人,包括Gmail联系人,我不希望包含gmail联系人。而且所花的时间也很慢。我如何优化这一点,我知道它花费时间因为我使用了很多游标..但是不知道如何制作一个可以给我姓名电子邮件号码照片的单个cusror ...谢谢!

更新的决赛:

private void getContacts() {

    ContentResolver cr = getContentResolver();

    Cursor cur = cr.query(Data.CONTENT_URI, new String[] { Data.CONTACT_ID, Data.MIMETYPE, Email.ADDRESS,
            Contacts.DISPLAY_NAME, Phone.NUMBER }, null, null, Contacts.DISPLAY_NAME);

    Contact contact;

    if (cur.getCount() > 0) {

        while (cur.moveToNext()) {

            String id = cur.getString(cur.getColumnIndex(Data.CONTACT_ID));

            String mimeType = cur.getString(cur.getColumnIndex(Data.MIMETYPE));

            if (allContacts.containsKey(id)) {
                // update contact
                contact = allContacts.get(id);
            } else {
                contact = new Contact();
                allContacts.put(id, contact);
                // set photoUri
                contact.setContactPhotoUri(getContactPhotoUri(Long.parseLong(id)));
            }

            if (mimeType.equals(StructuredName.CONTENT_ITEM_TYPE))
                // set name
                contact.setContactName(cur.getString(cur.getColumnIndex(Contacts.DISPLAY_NAME)));

            if (mimeType.equals(Phone.CONTENT_ITEM_TYPE))
                // set phone munber
                contact.setContactNumber(cur.getString(cur.getColumnIndex(Phone.NUMBER)));

            if (mimeType.equals(Email.CONTENT_ITEM_TYPE))
                // set email
                contact.setContactEmail(cur.getString(cur.getColumnIndex(Email.ADDRESS)));

        }
    }

    cur.close();
    // get contacts from hashmap
    contacts.clear();
    contacts.addAll(allContacts.values());

    // remove null contacts
    for (Contact _contact : contacts) {

        if (_contact.getContactName() == null && _contact.getContactNumber() == null
                && _contact.getContactEmail() == null) {
            contacts.remove(_contact);
            break;
        }

    }

    contactAdapter = new ContactAdapter(this, R.id.contactList, contacts);
    contactAdapter.notifyDataSetChanged();

}

public Uri getContactPhotoUri(long contactId) {
    Uri photoUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
    photoUri = Uri.withAppendedPath(photoUri, Contacts.Photo.CONTENT_DIRECTORY);
    return photoUri;
}

1 个答案:

答案 0 :(得分:23)

您应该能够在Data.CONTENT_URI上获得一个查询所需的所有信息, 查看" android.provider.ContactsContract.Data"表和如何查询不同类型的数据的例子电子邮件,电话,照片等... http://developer.android.com/reference/android/provider/ContactsContract.Data.html

例如:

Cursor data = cntx.getContentResolver().query(Data.CONTENT_URI, new String[] {Data._ID,Data.MIMETYPE,Email.ADDRESS,Photo.PHOTO},Data.CONTACT_ID + "=?" + " AND " + "(" +  Data.MIMETYPE + "='" + Photo.CONTENT_ITEM_TYPE + "' OR " + Data.MIMETYPE + "='" + Email.CONTENT_ITEM_TYPE +"')",
                        new String[] {String.valueOf(contactId)}, null);

应该为您提供有关某个特定contactId的所有信息,您理论上可以要求所有联系人并自行对信息进行排序。

至于过滤Gmail联系人这是一个更复杂的问题,请查看ACCOUNT_NAME / TYPE http://developer.android.com/reference/android/provider/ContactsContract.RawContacts.html 参数和有关此问题的讨论: What is the default Account Type / Name for contacts on Android Contact Application?