获取android联系人详细信息非常慢

时间:2013-07-22 22:34:19

标签: android performance sdk android-contacts contactscontract

我的应用程序有一个listView,由设备上注册的联系人填充,但是当我加载listView时,它需要太长时间!这是我的代码:

public List<Contact> getContactsList(Context context, ContentResolver contentResolver) {

    List<Contact> listContatos = new ArrayList<Contact>();
    Uri uri = ContactsContract.Contacts.CONTENT_URI;

    Cursor cursorContacts = contentResolver.query(uri, null, null, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE NOCASE ASC;");
    try {
        // while there is a next contact, retrieves your data
        while (cursorContacts.moveToNext()) {

            Contact c = getContact(context, cursorContacts);

            if (c != null) {
                listContatos.add(c);
            }
        }
    } finally {

        // Closes Cursor
        cursorContacts.close();
    }
    return listContatos;
}





public Contact getContact(Context context, Cursor contactsCur) {

    Contact c = new Contact();

    // get contact id
    long id = contactsCur.getLong(contactsCur.getColumnIndexOrThrow(BaseColumns._ID));
    String strId = contactsCur.getString(contactsCur.getColumnIndex(BaseColumns._ID));

    // get contact name
    String name = contactsCur.getString(contactsCur.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));     

    boolean temFone = false;
    // if return is different than 1, doesn't has phone number
    temFone = "1".equals(contactsCur.getString(contactsCur.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
    if (temFone) {
        contactsCur = context.getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                new String[] { strId }, null);

        ArrayList<PhoneNumber> phones = new ArrayList<PhoneNumber>();
        while (contactsCur.moveToNext()) {

            int idx = contactsCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

            if (idx != -1) {
                String phoneNumber = contactsCur.getString(contactsCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                int phonetype = contactsCur.getInt(contactsCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                String customLabel = contactsCur.getString(contactsCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL));
                String phoneLabel = (String) ContactsContract.CommonDataKinds.Phone.getTypeLabel(context.getResources(), phonetype, customLabel);

                phones.add(new PhoneNumber(phoneLabel, phoneNumber));                    
            }
        }
        c.setPhones(phones);

        contactsCur.close();
    } else {
        c.setPhones(null);
    }

    loadAddress(context, c, id);
    c.setContactId(id);
    c.setName(name);
    loadEmails(context, c, id);
    loadPhotoUri(context, c, id);
    return c;
}







private void loadEmails(Context context, Contact c, long id) {

    Cursor cursor = context.getContentResolver().query(
            ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
            ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + id,
            null, null);

    ArrayList<ContactMail> emails = new ArrayList<ContactMail>();
    while (cursor.moveToNext()) {
        int idx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);

        if (idx != -1) {
            String stremail = cursor.getString(idx);

            int emailtype = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
            String customLabel = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.LABEL));
            String emailLabel = (String) ContactsContract.CommonDataKinds.Email.getTypeLabel(context.getResources(), emailtype, customLabel);

            if (!stremail.equals(""))
                emails.add(new ContactMail(stremail, emailLabel));
        }
    }
    c.setEmails(emails);
    cursor.close();

}

private void loadAddress(Context context, Contact c, long id) {

    Cursor cursor = context.getContentResolver().query(
            ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, null, ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = " + id, null, null);

    ArrayList<Address> addresses = new ArrayList<Address>();
    while (cursor.moveToNext()) {
        int idx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.DATA);

        if (idx != -1) {                
            String address = cursor.getString(idx);

            int addresstype = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
            String customLabel = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.LABEL));
            String addressLabel = (String) ContactsContract.CommonDataKinds.StructuredPostal.getTypeLabel(context.getResources(), addresstype, customLabel);

            if (!address.equals(""))
                addresses.add(new Address(address, addressLabel, null));


        }
    }
    c.setAddresses(addresses);
    cursor.close();
}

public void loadPhotoUri(Context context, Contact c, long id) {

    Cursor cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
                    null,
                    ContactsContract.Data.CONTACT_ID
                            + "="
                            + id
                            + " AND "

                            + ContactsContract.Data.MIMETYPE
                            + "='"
                            + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
                            + "'", null, null);

    while (cursor.moveToNext()) {
        int idx = cursor
                .getColumnIndex(ContactsContract.CommonDataKinds.SipAddress.DATA);

        if (idx != -1) {
            Uri person = ContentUris.withAppendedId(
                    ContactsContract.Contacts.CONTENT_URI, id); 
            c.setPhoto( Uri.withAppendedPath(person,
                    ContactsContract.Contacts.Photo.CONTENT_DIRECTORY));
            }
        } 
    } 

它工作正常,完美,唯一的问题是需要永远获取所有联系人信息。我使用getContact方法获取有关设备中每个联系人的所有详细信息,而不是getContactsList,然后将其放入Contact列表中。 Contact列表由listView的适配器使用。

提前致谢!

对于任何英语错误感到抱歉......

3 个答案:

答案 0 :(得分:4)

经过这么多研究后,我发现这个解决方案100%快速加载。

加载与Cursor Loader的所有联系

String[] PROJECTION = new String[] {
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER };
    ArrayList<ContactBean> listContacts = new ArrayList<>();
    CursorLoader cursorLoader = new CursorLoader(context,
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION,
            null, null, "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME
                    + ")ASC");

    Cursor c = cursorLoader.loadInBackground();

    if (c.moveToFirst()) {

        int Number = c
                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        int Name = c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);

        do {

            try {
                String phNumber = c.getString(Number);
                String phName = c.getString(Name);




            } catch (Exception e) {

            }

        } while (c.moveToNext());

        c.close();
    }

答案 1 :(得分:3)

我遇到了同样的问题。不得不显示所有的联系人姓名和电话号码 - 工作得很好,但花了太长时间。

作为Android开发世界的新手,我做了一些关于它的研究,发现了以下帖子:

对我来说非常有用。

我认为问题在于您正在进行内部查询,因此它将永远带您。 我的代码的主要问题是多次获取列索引,(所以我把它改成了在while循环之前的一次),并且我在代码中找到了一个不太清晰的内部查询,并找到了一种方法把它拿出来。

尝试使用该帖子中的示例,看看它是否有帮助(希望它会 - 因为对我来说它是完美的!)

祝你好运

答案 2 :(得分:1)

这是我的快速解决方案:

Cursor cursorPhones = getContentResolver().query(
    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
    new String[]{
        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
        ContactsContract.CommonDataKinds.Phone.NUMBER,
        ContactsContract.CommonDataKinds.Phone.TYPE,
        ContactsContract.CommonDataKinds.Phone.LABEL
    },
    ContactsContract.Contacts.HAS_PHONE_NUMBER + ">0 AND LENGTH(" + ContactsContract.CommonDataKinds.Phone.NUMBER + ")>0",
    null,
    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
);

while (cursorPhones.moveToNext()) {

    String displayName = cursorPhones.getString(cursorPhones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
    String number = cursorPhones.getString(cursorPhones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    int type = cursorPhones.getInt(cursorPhones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
    String label = cursorPhones.getString(cursorPhones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL));
    CharSequence phoneLabel = ContactsContract.CommonDataKinds.Phone.getTypeLabel(context.getResources(), type, label);

}

cursorPhones.close();