-1

I have written code for importing all the contacts with details (Name,Mobile number,DOB,anniversary and email address) from device addressbook to my application database.The code is running fine and getting all details but its a very slow process for large number of contacts.For example on samsumg Galaxy s4 with 8000 contacts it takes upto 20 minutes.The code I am using is following:

ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                    null, null, null);
int count = cur.getCount();
if (cur.getCount() > 0) {
    while (cur.moveToNext()) {
first_name = "";
first_name = "";

                    name = cur
                            .getString(cur
                                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                    first_name = name == null ? "" : name;


                    // birthday
                    Cursor bdc = cr
                            .query(android.provider.ContactsContract.Data.CONTENT_URI,
                                    new String[] { Event.DATA },
                                    android.provider.ContactsContract.Data.CONTACT_ID
                                            + " = "
                                            + id
                                            + " AND "
                                            + Data.MIMETYPE
                                            + " = '"
                                            + Event.CONTENT_ITEM_TYPE
                                            + "' AND "
                                            + Event.TYPE
                                            + " = "
                                            + Event.TYPE_BIRTHDAY,
                                    null,
                                    android.provider.ContactsContract.Data.DISPLAY_NAME);
                    if (bdc.getCount() > 0) {
                        while (bdc.moveToNext()) {
                            birthDate = bdc.getString(0);
                        }
                    }

                  if (bdc != null)
                    bdc.close();

                Cursor ann = cr
                        .query(android.provider.ContactsContract.Data.CONTENT_URI,
                                new String[] { Event.DATA },
                                android.provider.ContactsContract.Data.CONTACT_ID
                                        + " = "
                                        + id
                                        + " AND "
                                        + Data.MIMETYPE
                                        + " = '"
                                        + Event.CONTENT_ITEM_TYPE
                                        + "' AND "
                                        + Event.TYPE
                                        + " = "
                                        + Event.TYPE_ANNIVERSARY,
                                null,
                                android.provider.ContactsContract.Data.DISPLAY_NAME);
                if (ann.getCount() > 0) {
                    while (ann.moveToNext()) {
                        anniversaryDate = ann.getString(0);
                    }
                }
               if (ann != null)
                    ann.close();


                Cursor cur1 = cr.query(
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID
                                + " = ?", new String[] { id }, null);

                if (cur1.moveToNext()) {
                    email1 = cur1
                            .getString(cur1
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    if (email1 == null)
                        email1 = "";
     if (cur1 != null)
            cur1.close();

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);
                    if (pCur.moveToNext()) {

                        phone = pCur
                                .getString(pCur
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        if (phone == null)
                            phone = "";
                    }

How Can I improve my above code to run more faster?Can someone guide me what am I doing wrong in my code?

Anshul
  • 7,914
  • 12
  • 42
  • 65

1 Answers1

0

put them in a new thread, after you get the result, give it to Handler to display on UI; It will not make your code faster, but at least, it won't stuck your UI.

gone
  • 823
  • 8
  • 21