Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting android contacts details very slow

My app has a listView populated by the contacts registered on the device, but when i load the listView, it takes too long! Here is my code:

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

It's working just fine, perfectly, the only problem is that takes forever to get all contacts information's. I use the getContact method to get all detailed info about every contact in the device, than in getContactsList I put then in a list of Contact. The list of Contact is used by the adapter of the listView.

Thanks in advance!

Sorry about any english mistakes...

like image 365
Gabriela Nogueira Avatar asked Jul 22 '13 22:07

Gabriela Nogueira


People also ask

Why is my android phone so slow all of a sudden?

A few easy-to-fix reasons why your android device or iPhone might be slow include: A need to clear your random-access memory (RAM) A low-performing battery. Low or no storage.

Why is my phone so laggy all of a sudden?

If your Android is running slow, chances are the issue can be quickly fixed by clearing out excess data stored in your phone's cache and deleting any unused apps. A slow Android phone may require a system update to get it back up to speed, although older phones may not be able to run the latest software properly.

How do you fix a slow phone?

Clear the cache data on your Android or your iPhone. Occasionally, the data your smartphone saves to make things move faster can slow things down. Clearing it can immediately fix smartphone issues and also free up space. Reset your Android to factory settings or reset your iPhone.


1 Answers

After so many Research I found this solution 100% fast loading.

Loading all contact with 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();
    }
like image 71
SahdevRajput74 Avatar answered Nov 07 '22 19:11

SahdevRajput74