1

我想将手机中的所有联系人放入一个数组中。我正在使用下面的代码,但将它们全部放入数组需要 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();
                }
            }
        }      

为什么这么慢?

4

1 回答 1

1

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

于 2011-10-18T19:24:55.110 回答