联系人加入 (Android M)

Contact Joining (Android M)

我尝试加入两个联系人,一个来自默认地址存储的默认联系人,一个是我自己的提供商的联系人。 我有以下 com.android.Contact 应用程序的代码片段:

private interface JoinContactQuery {
    String[] PROJECTION = {
        RawContacts._ID,
        RawContacts.CONTACT_ID,
        RawContacts.NAME_VERIFIED,
        RawContacts.DISPLAY_NAME_SOURCE,
    };

    String SELECTION = RawContacts.CONTACT_ID + "=? OR " + RawContacts.CONTACT_ID + "=?";

    int _ID = 0;
    int CONTACT_ID = 1;
    int NAME_VERIFIED = 2;
    int DISPLAY_NAME_SOURCE = 3;
}


Cursor c = resolver.query(ContactsContract.RawContacts.CONTENT_URI,
            JoinContactQuery.PROJECTION,
            JoinContactQuery.SELECTION,
        new String[]{String.valueOf(contactId1), String.valueOf(contactId2)}, null);

if (c == null) {
    Log.e(TAG, "Unable to open Contacts DB cursor");
    return;
}

long rawContactIds[];
long verifiedNameRawContactId = -1;
try {
    if (c.getCount() == 0) {
    return;
    }
    int maxDisplayNameSource = -1;
    rawContactIds = new long[c.getCount()];
    for (int i = 0; i < rawContactIds.length; i++) {
    c.moveToPosition(i);
    long rawContactId = c.getLong(JoinContactQuery._ID);
    rawContactIds[i] = rawContactId;
    int nameSource = c.getInt(JoinContactQuery.DISPLAY_NAME_SOURCE);
    if (nameSource > maxDisplayNameSource) {
        maxDisplayNameSource = nameSource;
    }
    }

    // Find an appropriate display name for the joined contact:
    // if should have a higher DisplayNameSource or be the name
    // of the original contact that we are joining with another.
    if (writable) {
    for (int i = 0; i < rawContactIds.length; i++) {
        c.moveToPosition(i);
        if (c.getLong(JoinContactQuery.CONTACT_ID) == contactId1) {
            int nameSource = c.getInt(JoinContactQuery.DISPLAY_NAME_SOURCE);
            if (nameSource == maxDisplayNameSource
                    && (verifiedNameRawContactId == -1
                            || c.getInt(JoinContactQuery.NAME_VERIFIED) != 0)) {
                verifiedNameRawContactId = c.getLong(JoinContactQuery._ID);
            }
        }
    }
    }
} finally {
    c.close();
}

// For each pair of raw contacts, insert an aggregation exception
ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
for (int i = 0; i < rawContactIds.length; i++) {
    for (int j = 0; j < rawContactIds.length; j++) {
    if (i != j) {
        buildJoinContactDiff(operations, rawContactIds[i], rawContactIds[j]);
    }
    }
}

// Mark the original contact as "name verified" to make sure that the contact
// display name does not change as a result of the join
if (verifiedNameRawContactId != -1) {
    Builder builder = ContentProviderOperation.newUpdate(
        ContentUris.withAppendedId(RawContacts.CONTENT_URI, verifiedNameRawContactId));
    builder.withValue("name_verified", 1);
    operations.add(builder.build());
}

我的问题:"name_verified" 字段已在 Android M(预览版 2)上删除。 "correct" 加入两个联系人的方法是什么(并且不更改联系人的姓名)?

* 编辑/解决方案 *

删除 name_verified 并将 "IS_SUPER_PRIMARY" 设置为内容 uri

    //mark as SUPER PRIMARY
    if (verifiedNameRawContactId != -1) {
        operations.add(
                ContentProviderOperation.newUpdate(ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, verifiedNameRawContactId))
                        .withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
                        .build());
    }

我不太清楚这个问题,但我认为你正在尝试加入两个联系人,一个名字优先于另一个名字,但不是你想要的方式。

加入联系人的正确方法是在此table中添加一行,并保持类型为TYPE_KEEP_TOGETHER。 AggregationsExceptions table

如果要保留第一个raw_contact的姓名,请在数据table(StructuredName)中将联系人的姓名记录标记为IS_SUPER_PRIMARY

IS_SUPER_PRIMARY