联系提取(姓名,手机号码,电子邮件地址和生日)需要花费太多时间

时间:2016-11-11 09:44:34

标签: android

我正在尝试取消与姓名,手机号码,电子邮件地址和生日的联系,但是操作花费了太多时间(顺便提一下设备有2000个联系人)。即使UI被挂起,如果我在自定义进度对话框中显示动画,它也不是动画。

(快速联系人提取或动画至少应该有效)。

(甚至,我已经使用asynctask进行联系提取和动画,但我得到了相同的结果)。

这是我的代码

ContactListClass.phoneList.clear();

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

        if (cur.getCount() > 0) {

            int progress = 0;
            // customIndeterminantDialog.setMax(cur.getCount());

            int i = 0;

            while (cur.moveToNext()) {

                ContactObject contactObject = new ContactObject();

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

//                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                // Query phone here. Covered next

                JSONObject jsonObject = new JSONObject();

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

                    // Do something with phones
                    phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    phoneNo = phoneNo.replaceAll("\\W+", "");
                }

//                Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null);
//
//                if (emailCur != null || emailCur.getCount() > 0) {
//
//                    while (emailCur.moveToNext()) {
//                        String emailContact = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
//                        if (emailContact != null) {
//                            emailId = emailContact;
//
//                        } else {
//                            emailId = "";
//                        }
//                    }
//                    emailCur.close();
//                }

                contactObject.setName(name);

                contactObject.setphone(phoneNo);
                contactObject.setEmail(emailId);

                contactObjectsList.add(contactObject);

                //customIndeterminantDialog.setProgress(++progress);

                pCur.close();
//                }
                i++;
            }

            customIndeterminantDialog.dismiss();

        } else {
            customIndeterminantDialog.dismiss();
        }

1 个答案:

答案 0 :(得分:0)

使用AsyncTaskLoader

解决了该问题