如何使其正确聚集

如何使其正确聚集

本文介绍了以编程方式添加联系人时,如何使其正确聚集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了问题和答案,但是添加电话信息(甚至电子邮件)仍无法正确汇总联系信息(当我检查人脉应用时,我可以看到多个同名条目)。

I saw this question and answer, but adding the phone information (and even email) still does not cause the contact information to aggregate properly (when I check the People app, I can see multiple entries under the same name).

这是我用来测试它的代码。

Here is the code I use to test it.

//get the account
Account acct = null;
Account[] accounts = AccountManager.get(getContext()).getAccounts();
for (Account acc : accounts){
    acct = acc;
}//assuming there's only one account in there (in my case I know there is)

//loop a few times, creating a new contact each time. In theory, if they have the same name they should aggregate
for(int i=0; i<3; i++){
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, acct.type)
                .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, acct.name)
                .withValue(ContactsContract.RawContacts.AGGREGATION_MODE, ContactsContract.RawContacts.AGGREGATION_MODE_DEFAULT)
                .build());
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "ContactName")
                .build());
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, "1234567890")
                .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 1)
                .build());
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Email.DATA, "[email protected]")
                .withValue(ContactsContract.CommonDataKinds.Email.TYPE, 1)
                .build());

    try{
        getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    }
    catch (Exception e) {
        Log.e("Contacts", "Something went wrong during creation! " + e);
        e.printStackTrace();
    }
}


推荐答案

如果它们不会自动聚合,您可以通过在 AggregationExceptions 表格。确保您在文档中注意到不允许插入。您必须进行更新。现在让我抓到了两次。以下代码应汇总ID为1和2的两个原始联系人。

If they aren't aggregating automatically, you can aggregate them manually by adding a row to the AggregationExceptions table. Make sure you notice in the docs that insert is not allowed. You have to do an update instead. That's caught me twice now. The following code should aggregate the two raw contacts with id's 1 and 2:

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);

这篇关于以编程方式添加联系人时,如何使其正确聚集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 07:53