Android ContactsContract.Contacts 慢

发布于 2024-12-10 12:09:41 字数 1299 浏览 3 评论 0原文

我想将手机中的所有联系人放入一个数组中。我正在使用下面的代码,但将它们全部放入数组需要 3-4 秒。为了加快这一过程,我只收集那些拥有电话号码的联系人。这会产生一个包含 163 个元素的数组。

final String[] projection1 = new String[] {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.HAS_PHONE_NUMBER};
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, projection1, null, null, null);
        if (cur.getCount() > 0) 
        {
            while (cur.moveToNext()) {
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                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()) {
                    number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    contactnumbers.add(number);

                } 
                pCur.close();
                }
            }
        }      

为什么这么慢?

I want to put all the contacts in my phone into an array. I am using the code below but it takes 3-4 seconds to put all of them into the array. To accelerate the process, I am collecting only those contacts who has a phone number. This results in an array of 163 elements.

final String[] projection1 = new String[] {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.HAS_PHONE_NUMBER};
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, projection1, null, null, null);
        if (cur.getCount() > 0) 
        {
            while (cur.moveToNext()) {
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                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()) {
                    number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    contactnumbers.add(number);

                } 
                pCur.close();
                }
            }
        }      

Why is this so slow?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟燃烟灭 2024-12-17 12:09:41

您在查询中有一个查询。为什么不创建一个连接查询来连接两个语句。这可能比当前的解决方案快得多。

You have a query inside a query. Why don't you create a joined query where you join both statements. This will be probably much faster than the current solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文