如何一起从本地电话簿和Google联系人中获取所有联系人? [英] How to fetch all contacts from local phonebook and google contacts together?

查看:114
本文介绍了如何一起从本地电话簿和Google联系人中获取所有联系人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Android应用程序中的电话簿中获取联系人.但是它将获取仅存在于本地电话存储中的联系人.我需要获取所有联系人,包括使用各种帐户(例如Google)同步到设备的联系人.目前还没有发生.我正在使用RecyclerView来显示获取的联系人.

I am trying to fetch contacts from the phonebook in my Android application. But it fetches the contacts that are present only in the local phone storage. I need to fetch all the contacts including the ones synced to the device using various accounts like Google. That is currently not happening. I am using a RecyclerView to display the contacts fetched.

我尝试使用 https://github.com/mirrajabi/rx-contacts2 用于异步获取的库.但这还不包括Google联系人.然后我尝试使用Android内置的CotentResolver

I have tried using https://github.com/mirrajabi/rx-contacts2 library for fetching asynchronously. But that doesn't include Google contacts as well. Then I tried using Android's built-in CotentResolver

Contact contact;

        ContentResolver contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
        if (cursor != null) {
            if (cursor.getCount() > 0) {
                while (cursor.moveToNext()) {

                    int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
                    if (hasPhoneNumber > 0) {
                        String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                        contact = new Contact(Long.parseLong(id));
                        contact.setDisplayName(name);

                        Cursor phoneCursor = contentResolver.query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                null,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                                new String[]{id},
                                null);
                        if (phoneCursor != null) {
                            if (phoneCursor.moveToNext()) {
                                String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                Set<String> phoneNumbers = new HashSet<>();
                                phoneNumbers.add(phoneNumber);
                                contact.setPhoneNumbers(phoneNumbers);
                            }
                            phoneCursor.close();
                        }


                        Cursor emailCursor = contentResolver.query(
                                ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                                null,
                                ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                                new String[]{id}, null);
                        if (emailCursor != null) {
                            while (emailCursor.moveToNext()) {
                                String emailId = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                            }
                            emailCursor.close();
                        }
                        listContacts.add(contact);
                    }
                }
            }
        cursor.close();
}

当前,我正在尝试同步获取联系人,它使主线程挂起.如果您可以提出一些异步执行此操作的方法,那将非常有帮助.这样做时,我还需要触发器来知道任务何时完成.

Currently, I am trying to fetch the contacts synchronously and it hangs up the main thread. It would be really helpful if you could suggest some ways to do that asynchronously. When doing so I also require a trigger to know when the task is completed.

推荐答案

您的代码应适用于同步到设备的所有联系人,包括Google联系人(假设已安装Google帐户,并且启用了联系人同步).

Your code should work on all contacts synced to the device, including Google contacts (assuming the Google account is installed, and the contacts sync is enabled).

但是,您的代码有一些错误,可以大大改善,目前对于具有500个联系人的设备,您正在执行约1000个查询. 您需要的所有数据都在称为Data的单个表上,因此您可以在单个快速查询中获得所有内容,请参见此处:

However, your code has some bugs, and can be greatly improved, currently for a device with 500 contacts, you are doing ~1000 queries. All the data you need is on a single table called Data so you can get everything in a single quick query, see here:

Map<Long, Contact> contacts = new HashMap<>();

String[] projection = {Data.CONTACT_ID, Data.DISPLAY_NAME, Data.MIMETYPE, Data.DATA1};
String selection = Data.MIMETYPE + " IN ('" + Phone.CONTENT_ITEM_TYPE + "', '" + Email.CONTENT_ITEM_TYPE + "')";
Cursor cur = cr.query(Data.CONTENT_URI, projection, selection, null, null);

while (cur.moveToNext()) {
    long id = cur.getLong(0);
    String name = cur.getString(1);
    String mime = cur.getString(2); // email / phone
    String data = cur.getString(3); // the actual info, e.g. +1-212-555-1234

    // get the Contact class from the HashMap, or create a new one and add it to the Hash
    Contact contact;
    if (contacts.containsKey(id)) {
        contact = contacts.get(id);
    } else {
        contact = new Contact(id);
        contact.setDisplayName(name);
        // start with empty Sets for phones and emails
        contact.setPhoneNumbers(new HashSet<>());
        contact.setEmails(new HashSet<>());
        contacts.put(id, contact);
    } 

    switch (mime) {
        case Phone.CONTENT_ITEM_TYPE: 
            contact.getPhoneNumbers().add(data);
            break;
        case Email.CONTENT_ITEM_TYPE: 
            contact.getEmails().add(data);
            break;
    }
}
cur.close();

注意:

  1. 我已将您的listContacts更改为名为contacts的HashMap,以便我们可以快速找到现有联系人
  2. 我已将setEmailsgetPhoneNumbersgetEmails添加到您的Contact
  1. I've changed your listContacts to a HashMap called contacts so we can quickly find an existing contact
  2. I've added setEmails, getPhoneNumbers and getEmails to your Contact class

这篇关于如何一起从本地电话簿和Google联系人中获取所有联系人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆