一个新的应用程序,我需要用手机导入所有联系人,
我在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;

最佳答案

使用其他资源和一些常识进行搜索后,获取设备中所有移动电话的答案是查询contactscontract.commondatakinds.phone.content_uri,而不是contactscontract.data.content_uri,然后在上面运行光标。

10-07 19:20
查看更多