加载所有联系人和所有电话号码的有效方法(Android 2.0)

发布于 2024-12-01 08:50:09 字数 1008 浏览 0 评论 0原文

有没有一种方法可以获取所有联系人的所有电话号码,而无需对每个联系人进行单独的查询? (使用Android 2.0+)。如果您有超过 100 个联系人(在低端手机上无法使用),它真的很慢,我想知道是否可以进行更有效的查询。

目前,我正在获取包含所有有效 ContactsContract.Contacts.IN_VISIBLE_GROUP 联系人的游标,然后对每个联系人进行单独的查询以获取其所有号码。

来自获取联系人的片段仅获取姓名和查找键:

Uri uri = ContactsContract.Contacts.CONTENT_URI;

String[] projection = new String[] {
    ContactsContract.Contacts.DISPLAY_NAME,
    ContactsContract.Contacts.LOOKUP_KEY};

然后每个联系人都使用查找键。

Uri lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
Uri res = ContactsContract.Contacts.lookupContact(contentResolver, lookupUri);

String[] projection =  new String[]{ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER};

...

Cursor phones = contentResolver.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, phoneProjection, selection, selectionArgs, sortOrder);   

Is there a way of getting all the phone numbers for all contacts without doing a separate query for each contact? (using Android 2.0+). It's really slow if you have over 100 contacts (unusable on low end phones), I wondered if I could do a more efficient query.

Currently I'm getting a cursor with all valid ContactsContract.Contacts.IN_VISIBLE_GROUP contacts then a separate query for each contact to get all their numbers.

Snippet from the get Contacts just geting name and lookup key:

Uri uri = ContactsContract.Contacts.CONTENT_URI;

String[] projection = new String[] {
    ContactsContract.Contacts.DISPLAY_NAME,
    ContactsContract.Contacts.LOOKUP_KEY};

Then each contact using the lookup key.

Uri lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
Uri res = ContactsContract.Contacts.lookupContact(contentResolver, lookupUri);

String[] projection =  new String[]{ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER};

...

Cursor phones = contentResolver.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, phoneProjection, selection, selectionArgs, sortOrder);   

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

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

发布评论

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

评论(1

少钕鈤記 2024-12-08 08:50:09

检查以下代码是否有帮助

    public ArrayList<PhoneContactInfo> getAllPhoneContacts() {
    Log.d("START","Getting all Contacts");
    ArrayList<PhoneContactInfo> arrContacts = new ArrayList<PhoneContactInfo>();
    PhoneContactInfo phoneContactInfo=null;     
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    Cursor cursor = context.getContentResolver().query(uri, new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone._ID}, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
    cursor.moveToFirst();
    while (cursor.isAfterLast() == false)
    {
        String contactNumber= cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));  
        String contactName =  cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        int phoneContactID = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));


        phoneContactInfo = new PhoneContactInfo();
        phoneContactInfo.setPhoneContactID(phoneContactID);             
        phoneContactInfo.setContactName(contactName);                   
        phoneContactInfo.setContactNumber(contactNumber); 
        if (phoneContactInfo != null)
        {
            arrContacts.add(phoneContactInfo);
        }
        phoneContactInfo = null; 
        cursor.moveToNext();
    }       
    cursor.close();
    cursor = null;
    Log.d("END","Got all Contacts");
    return arrContacts;
}

Check if the below code helps

    public ArrayList<PhoneContactInfo> getAllPhoneContacts() {
    Log.d("START","Getting all Contacts");
    ArrayList<PhoneContactInfo> arrContacts = new ArrayList<PhoneContactInfo>();
    PhoneContactInfo phoneContactInfo=null;     
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    Cursor cursor = context.getContentResolver().query(uri, new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone._ID}, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
    cursor.moveToFirst();
    while (cursor.isAfterLast() == false)
    {
        String contactNumber= cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));  
        String contactName =  cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        int phoneContactID = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));


        phoneContactInfo = new PhoneContactInfo();
        phoneContactInfo.setPhoneContactID(phoneContactID);             
        phoneContactInfo.setContactName(contactName);                   
        phoneContactInfo.setContactNumber(contactNumber); 
        if (phoneContactInfo != null)
        {
            arrContacts.add(phoneContactInfo);
        }
        phoneContactInfo = null; 
        cursor.moveToNext();
    }       
    cursor.close();
    cursor = null;
    Log.d("END","Got all Contacts");
    return arrContacts;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文