创建新联系人,而不是更新现有联系人 [英] New contact creating rather than updating existing contact

查看:111
本文介绍了创建新联系人,而不是更新现有联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的应用程序与android默认的Contacts应用程序集成。我想在每个Contacts Detail中显示一个选项 xyz using MyApp。我可以在Accounts Section(帐户部分)中看到我的应用程序,并带有一个同步Contacts的选项,但我仍然应用程序不与现有联系人合并,而是创建一个新联系人并在其中合并。

I am integrating my app with android default Contacts application.I would like to show an option "xyz using MyApp" inside every Contacts Detail.I am able to see my app in Accounts Section with an option to sync Contacts but still my app not merging with existing contacts but instead creating a new contact and merging in it.

performSync()方法

performSync() method

private static void addContact(ContentResolver contentResolver,int name, int phoneNumber) {
    Log.i("XYZ", "Adding contact: " + name);
    ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();

    //Create our RawContact
    ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI);
    builder.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, name);
    builder.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.example.xyz.myapplication");
    builder.withValue(ContactsContract.RawContacts.SYNC1, phoneNumber);
    operationList.add(builder.build());

    //Create a Data record of common type 'StructuredName' for our RawContact
    builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
    builder.withValueBackReference(ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID, 0);
    builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
    builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name);
    operationList.add(builder.build());

    //Create a Data record of custom type "vnd.android.cursor.item/vnd.com.example.xyz.myapplication.profile" to display a link to the Last.fm profile
    builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
    builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0);
    builder.withValue(ContactsContract.Data.MIMETYPE, "vnd.android.cursor.item/vnd.com.example.xyz.myapplication.profile");
    builder.withValue(ContactsContract.Data.DATA1, phoneNumber);
    builder.withValue(ContactsContract.Data.DATA2, "Last.fm Profile");
    builder.withValue(ContactsContract.Data.DATA3, "View profile");
    operationList.add(builder.build());

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


推荐答案

您的 addContact 代码中缺少告诉 Contacts DB 将新的原始联系人加入现有联系人的部分,这样该联系人现在将包含您的原始联系人,并且在通讯录应用中打开该联系人时将显示您的应用专用行。

in your addContact code you're missing the part that tells Contacts DB to join your new raw-contact into the existing contact, so that contact will now contain your raw-contact and your app-specific line will be shown when opening that contact in the contacts app.

请查看此答案,以了解如何将 RawContact 加入现有的联系人中:为什么不汇总联系人?

Check this answer on how to join a RawContact into an existing Contact: why won't contacts aggregate?

您可能需要传递一些 RawContact ID 添加到您的addContact方法中,这样便可以将两者结合在一起。

You'll probably need to pass in some RawContact ID to your addContact method so it'll be able to join the two together.

UPDATE

不是将聚合操作与您的 RawContact 插入操作一起应用,而是尝试将其分为两个 applyBatch 通话,也让我们将您的新原始联系人与 ALL 现有原始联系人进行汇总,而不仅仅是其中之一。
尝试以下代码,确保将现有的联系人ID (不是原始联系人ID)和新的原始联系人ID 传递给它。

Instead of applying the aggregation operation together with your RawContact insert operation, let's try to separate into two applyBatch calls, also, let's aggregate your new raw-contact with ALL existing raw-contacts, not just one of them. Try the following code, make sure you pass to it the existing contact-id (not raw-contact id) and your new raw-contact-id.

private void joinIntoExistingContact(long existingContactId, long newRawContactId) {

    // get all existing raw-contact-ids that belong to the contact-id
    List<Long> existingRawIds = new ArrayList<>();
    Cursor cur = getContentResolver().query(RawContacts.CONTENT_URI, new String[] { RawContacts._ID }, RawContacts.CONTACT_ID + "=" + existingContactId, null, null);
    while (cur.moveToNext()) {
        existingRawIds.add(cur.getLong(0));
    }
    cur.close();
    Log.i("Join", "Found " + existingRawIds.size() + " raw-contact-ids");

    List<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    // go over all existing raw ids, and join with our new one
    for (Long existingRawId : existingRawIds) {
        Builder builder = ContentProviderOperation.newUpdate(AggregationExceptions.CONTENT_URI);
        builder.withValue(AggregationExceptions.TYPE, AggregationExceptions.TYPE_KEEP_TOGETHER);
        builder.withValue(AggregationExceptions.RAW_CONTACT_ID1, newRawContactId);
        builder.withValue(AggregationExceptions.RAW_CONTACT_ID2, existingRawId);
        ops.add(builder.build());
    }

    contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
}

PS

打开两个重复问题 ,一个就足够了。

P.S.
don't open two duplicate questions, one is enough.

另一个更新

您似乎有些困惑关于ID。

You seem to have some confusion about IDs.

个数据 ID, RawContact ID和 Contact ID。

There are Data IDs, RawContact IDs, and Contact IDs.

CommonDataKinds.Phone._ID 将返回 Data ID,标识存储该电话号码的数据表中的特定行。

CommonDataKinds.Phone._ID will return a Data ID, identifying the specific row in the Data table in which that phone number is stored.

您也可以从 Phone 表中获得其他ID,请使用:
CommonDataKinds.Phone.RAW_CONTACT_ID
CommonDataKinds.Phone.CONTACT_ID

You can get a from the Phone table the other IDs as well, use: CommonDataKinds.Phone.RAW_CONTACT_ID CommonDataKinds.Phone.CONTACT_ID

您可以在此处阅读更多内容:
https://stackoverflow.com/a/50084029/8 19355

You can read more here: https://stackoverflow.com/a/50084029/819355

这篇关于创建新联系人,而不是更新现有联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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