安卓软件开发工具包(Android SDK)-获取联系人速度缓慢。

3
一个新的应用,我需要导入所有手机联系人。
我在一个AsyncTask类中运行了以下代码。
它能正常工作,但非常慢,在一个有2000个联系人的设备上,设备会冻结几秒钟。 我知道可以更快地完成,因为有很多应用程序使用联系人。
有什么想法吗?
    public ArrayList<ContactInfo> getContacts() {
        ArrayList<ContactInfo> arrayList = new ArrayList<ContactInfo>();
        ContentResolver cr = GlobalData.instance().getContext().getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, Phone.DISPLAY_NAME + " ASC");
        String id;
        String name;
        int counter = 0;

    if (cur.getCount() > 0) {
        int indexId= cur.getColumnIndex(ContactsContract.Contacts._ID);
        int indexName = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
        int indexHasPhoneNum = cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
        Log.d("getContacts", "Start");
        while (cur.moveToNext()) {
            id = cur.getString(indexId);
            name = cur.getString(indexName);
            if (Integer.parseInt(cur.getString(indexHasPhoneNum)) > 0) {
                // Query phone here. Covered next
                Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id, null, null);
                while (phones.moveToNext()) {                   
                    int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
                    if(type == Phone.TYPE_MOBILE){
                        String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                        arrayList.add(new ContactInfo(name, phoneNumber));
                        counter++;
                    }
                }
                phones.close();
            }

        }
        Log.d("getContacts", "End (" + counter + ")" );
    }
    cur.close();
    return arrayList;
1个回答

3

在使用其他资源和一些常识进行搜索后,获取设备中所有手机的答案是查询ContactsContract.CommonDataKinds.Phone.CONTENT_URI而不是ContactsContract.Data.CONTENT_URI,并在其上运行光标。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接