使用一个查询加载姓名,电话和图片的联系人的有效方法

时间:2014-06-26 03:42:00

标签: android sqlite android-sqlite android-contentprovider android-contacts

我有以下代码可以在samsung s3设备上加载手机和图片的所有联系人。

public static void getAllContactWithNumberAndNameAndPhoto(Context context,
            ArrayList<ContactInfo> mContactList, boolean starred) {

        ContentResolver cr = context.getContentResolver();

        Cursor cur = null;
        if (starred == true) {
            cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                    "starred=?", new String[] { "1" }, null);
        } else {

            cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null,
                    null, null);
        }
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {

                ContactInfo item = new ContactInfo();
                String id = cur.getString(cur
                        .getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur
                        .getString(cur
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                // Uri photo = PhoneUtils.getPhotoUriFromID(context, id);
                String starredValue = cur.getString(cur
                        .getColumnIndex(ContactsContract.Contacts.STARRED));
                boolean isFav = false;
                if (starredValue.equals("1"))
                    isFav = true;

                if (Integer
                        .parseInt(cur.getString(cur
                                .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    Cursor pCur = cr.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                    + " = ?", new String[] { id }, null);
                    while (pCur.moveToNext()) {

                        String phoneNo = pCur
                                .getString(pCur
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        item.addPhone(removeCharactersFromPhoneNumber(phoneNo));
                    }
                    pCur.close();

                    // if (photo != null) {
                    //
                    // item.setPhoto(photo.toString());
                    // }

                    item.setName(name);
                    item.setFavorite(isFav);
                    item.setRecent(false);

                    mContactList.add(item);
                }
            }
            cur.close();
        }
    }

当我在1000个联系人上运行此代码时,加载大约需要40秒 当我删除为同一个1000个联系人加载多部手机的部分时,大约需要1.5秒。

任何人都可以告诉我是否有一种有效的方式加载联系人电话而不让用户等待所有这些时间。

1 个答案:

答案 0 :(得分:-1)

试试这个:

private Cursor getContacts() {
    // Run query
    Uri uri = ContactsContract.Contacts.CONTENT_URI;
    String[] projection = new String[] { ContactsContract.Contacts._ID,
        ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.PHOTO_URI, ContactsContract.Contacts.PHOTO_THUMBNAIL_URI, ContactsContract.CommonDataKinds.Phone.CONTENT_URI }; // put the items u need here
    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
        + ("1") + "'";
    String[] selectionArgs = null;
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
        + " COLLATE LOCALIZED ASC";
    return getContentResolver().query(uri, projection, selection, selectionArgs,
        sortOrder);
  }