我如何以编程方式合并两个不同的联系人?(Android)

时间:2016-10-01 09:26:12

标签: android merge contacts

我想以编程方式将具有不同联系信息的同名联系人合并到一个联系人到电话联系人列表。

我还检查了this link并看到了此answer

我使用了以下代码:

ContentValues cv = new ContentValues();
cv.put(AggregationExceptions.TYPE, AggregationExceptions.TYPE_KEEP_TOGETHER);
cv.put(AggregationExceptions.RAW_CONTACT_ID1, 1);
cv.put(AggregationExceptions.RAW_CONTACT_ID2, 2);
getContentResolver().update(AggregationExceptions.CONTENT_URI, cv, null, null);

我还尝试过一种方式,首先获取列表中联系人的所有信息并删除所有联系人。之后,添加一个新联系人,其中包含我使用预定义 contactId 的所有信息。这会删除所有联系人,但新联系人不会添加预定义的 contactId

您可以看到here

3 个答案:

答案 0 :(得分:2)

我假设您已经获取了联系人并构建了合并两个联系人的逻辑。

假设您要手动合并两个ContactsContract.RawContacts._IDmasterContactId contactIdArrayList < ContentProviderOperation > ops = new ArrayList < ContentProviderOperation > (); ops.add(ContentProviderOperation.newUpdate(ContactsContract.AggregationExceptions.CONTENT_URI) .withValue(ContactsContract.AggregationExceptions.TYPE, ContactsContract.AggregationExceptions.TYPE_KEEP_TOGETHER) .withValue(ContactsContract.AggregationExceptions.RAW_CONTACT_ID1, masterContactId) .withValue(ContactsContract.AggregationExceptions.RAW_CONTACT_ID2, contactId).build()); try { getApplicationContext().getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); } catch (RemoteException e) { e.printStackTrace(); } catch (OperationApplicationException e) { e.printStackTrace(); } 的联系人。

现在您可以通过以下代码进行合并:

class General{

    public function loadCache(){}
    public function storeCache(){}

    public function setSession(){}
    public function getSession(){}
}

答案 1 :(得分:0)

如果要在SQLite数据库中插入所有联系人,则可以使用SQLite查询来获取同名联系人。

获取同名联系人后,从联系人列表中删除旧联系人,并添加包含您应用中合并详细信息的新联系人。

答案 2 :(得分:0)

说明

https://developer.android.com/guide/topics/providers/contacts-provider
有3个表保存联系人的数据:

ContactsContract.Contacts
_ID = Unique Id
|----------------------------------|
| _ID | PHOTO_ID | PHOTO_URI | ... |
|----------------------------------|
|  1  | ...      | ...       | ... |
| ... | ...      | ...       | ... |
|----------------------------------|

ContactsContract.RawContacts
_ID        = Unique Id
CONTACT_ID = ContactsContract.Contacts._ID
|------------------------------------------------------|
| _ID | CONTACT_ID | ACCOUNT_NAME | ACCOUNT_TYPE | ... |
|------------------------------------------------------|
|  1  |     1      | +49012345678 | com.whatsapp | ... |
|  2  |     1      | xx@google.de | com.google   | ... |
|------------------------------------------------------|

ContactsContract.Data
_ID            = Unique Id
CONTACT_ID     = ContactsContract.Contacts._ID
RAW_CONTACT_ID = ContactsContract.RawContacts._ID
|-------------------------------------------------------------------|
| _ID | CONTACT_ID | RAW_CONTACT_ID | MIMETYPE | DATA1        | ... |
|-------------------------------------------------------------------|
|  1  |     1      |        1       | PHONE    | +49012345678 | ... |
|  2  |     1      |        2       | PHONE    | +49987654321 | ... |
|-------------------------------------------------------------------|

示例

假设您有3位John Doe联系人。

  • 一个设备联系人
  • 一个whatsApp联系人
  • 一个电报联系人
Device-Contact
_id    raw_contact_id  contact_id  account_type            mimetype                                                            data1                         data2             data3  
9856   1888            1916        com.htc.android.pcsc    vnd.android.cursor.item/name                                        John Doe                      John              Doe    
12259  1888            1916        com.htc.android.pcsc    vnd.android.cursor.item/phone_v2                                    +49 1511 0000000              2      

Telegram-Contact 
_id    raw_contact_id  contact_id  account_type            mimetype                                                            data1                         data2             data3          
11079  2253            2305        org.telegram.messenger  vnd.android.cursor.item/name                                        John Doe                      John              Doe            
11080  2253            2305        org.telegram.messenger  vnd.android.cursor.item/vnd.org.telegram.messenger.android.profile  123456789                     Telegram Profile  +4915110000000 

WhatsApp-Contact
_id    raw_contact_id  contact_id  account_type            mimetype                                                            data1                         data2             data3                            
12254  2483            2543        com.whatsapp            vnd.android.cursor.item/name                                        John Doe                      John              Doe                             
12255  2483            2543        com.whatsapp            vnd.android.cursor.item/vnd.com.whatsapp.profile                    4915110000000@s.whatsapp.net  WhatsApp          Nachricht an +49 1511 0000000    
12256  2483            2543        com.whatsapp            vnd.android.cursor.item/vnd.com.whatsapp.voip.call                  4915110000000@s.whatsapp.net  WhatsApp          Sprachanruf an +49 1511 0000000  
12257  2483            2543        com.whatsapp            vnd.android.cursor.item/vnd.com.whatsapp.video.call                 4915110000000@s.whatsapp.net  WhatsApp          Videoanruf an +49 1511 0000000   
12258  2483            2543        com.whatsapp            vnd.android.cursor.item/phone_v2                                    +4915110000000                2                 null                             

然后,以下方法将合并这3个联系人:

private static void mergeContacts(ArrayList<Contact> contacts, ContentResolver contentResolver) {
    //make sure is sorted in ascending order.
    Collections.sort(contacts, (o1, o2) -> o1.getContactId().compareTo(o2.getContactId()));

    final String rawId = contacts.get(0).getRawContactId(); //ContactsContract.RawContacts._ID
    ArrayList<ContentProviderOperation> ops = new ArrayList<>();
    Contact currentContact;

    for (int index = 1; index < contacts.size(); ++index) {
        currentContact = contacts.get(index);
        ops.add(ContentProviderOperation.newUpdate(ContactsContract.AggregationExceptions.CONTENT_URI)
                .withValue(ContactsContract.AggregationExceptions.TYPE, ContactsContract.AggregationExceptions.TYPE_KEEP_TOGETHER)
                .withValue(ContactsContract.AggregationExceptions.RAW_CONTACT_ID1, rawId)
                .withValue(ContactsContract.AggregationExceptions.RAW_CONTACT_ID2, currentContact.getRawContactId()).build());

        Log.d(TAG, "merge contacts: " + rawId + " - " + currentContact.getRawContactId());
    }

    if (!ops.isEmpty()) {
        try {
            ContentProviderResult[] result = contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
            Log.d(TAG, "result: " + result.length);
        } catch (Exception e) {
            Log.e(TAG, e.toString(), e);
        }
    }
}
}

结果

这是合并的联系人:

_id    raw_contact_id  contact_id  account_type            mimetype                                                            data1                         data2             data3                           
9856   1888            2543        com.htc.android.pcsc    vnd.android.cursor.item/name                                        John Doe                      John              Doe                            
12259  1888            2543        com.htc.android.pcsc    vnd.android.cursor.item/phone_v2                                    +49 1511 0000000              2                 null                            
11079  2253            2543        org.telegram.messenger  vnd.android.cursor.item/name                                        John Doe                      John              Doe                            
11080  2253            2543        org.telegram.messenger  vnd.android.cursor.item/vnd.org.telegram.messenger.android.profile  123456789                     Telegram Profile  +4915110000000                  
12254  2483            2543        com.whatsapp            vnd.android.cursor.item/name                                        John Doe                      John              Doe                            
12255  2483            2543        com.whatsapp            vnd.android.cursor.item/vnd.com.whatsapp.profile                    4915110000000@s.whatsapp.net  WhatsApp          Nachricht an +49 1511 0000000   
12256  2483            2543        com.whatsapp            vnd.android.cursor.item/vnd.com.whatsapp.voip.call                  4915110000000@s.whatsapp.net  WhatsApp          Sprachanruf an +49 1511 0000000 
12257  2483            2543        com.whatsapp            vnd.android.cursor.item/vnd.com.whatsapp.video.call                 4915110000000@s.whatsapp.net  WhatsApp          Videoanruf an +49 1511 0000000  
12258  2483            2543        com.whatsapp            vnd.android.cursor.item/phone_v2                                    +4915110000000                2                 null