gpt4 book ai didi

android - 添加 RawContact 以便它聚合到现有联系人

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:59:35 25 4
gpt4 key购买 nike

我正在尝试向现有联系人添加新的 RawContact,以便我的自定义数据字段显示在原始联系人中。我尝试使用与原始 RawContact 的 DisplayName 匹配的 DisplayName 将 StructuredName 数据行添加到我的新 RawContact。我认为匹配 DisplayNames 足以聚合两个 RawContacts,但联系人应用程序似乎将两个 RawContacts 显示为不同的联系人。

这是我的代码

  public static void addContact(Context context, Account account, String number, String displayname) {
Log.e(Global.TAG, "adding contact: " + number + " / " + displayname);

ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();

//Create our RawContact
ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);
builder.withValue(RawContacts.ACCOUNT_NAME, account.name);
builder.withValue(RawContacts.ACCOUNT_TYPE, account.type);

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, displayname);
operationList.add(builder.build());

//Create a Data record of custom type "vnd.android.cursor.item/vnd.be.ourservice.profile" to display a link to the 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.be.ourservice.profile");
builder.withValue(ContactsContract.Data.DATA1, number);
builder.withValue(ContactsContract.Data.DATA2, "ourservice user");
builder.withValue(ContactsContract.Data.DATA3, "Go to ourservice profile");
operationList.add(builder.build());



try {
mContentResolver.applyBatch(ContactsContract.AUTHORITY, operationList);
Log.d(Global.TAG, "addContact batch applied");
} catch (Exception e) {
Log.e(Global.TAG, "Something went wrong during creation! " + e);
e.printStackTrace();
}
}

最佳答案

我想通了。我尝试用包含与原始联系人相同号码的 CommonDataKinds.Phone 行替换 CommonDataKinds.StructuredName 行,然后它正确聚合 RawContacts。

工作代码如下所示

public static void addContactTag(Context context, Account account, String number) {

ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();

// Create our RawContact
ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);
builder.withValue(RawContacts.ACCOUNT_NAME, account.name);
builder.withValue(RawContacts.ACCOUNT_TYPE, account.type);
operationList.add(builder.build());

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

//Create a Data record of custom type "vnd.android.cursor.item/vnd.be.ourservice.profile" to display a link to our profile
builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
builder.withValueBackReference(Data.RAW_CONTACT_ID, 0);
builder.withValue(ContactsContract.Data.MIMETYPE, OURSERVICE_MIMETYPE);
builder.withValue(ContactsContract.Data.DATA1, number);
builder.withValue(ContactsContract.Data.DATA2, "ourservice user");
builder.withValue(ContactsContract.Data.DATA3, "Go to ourservice profile");
operationList.add(builder.build());


try {
mContentResolver.applyBatch(ContactsContract.AUTHORITY, operationList);
Log.d(Global.TAG, "addContact batch applied");
} catch (Exception e) {
Log.e(Global.TAG, "Something went wrong during creation! " + e);
e.printStackTrace();
}
}

关于android - 添加 RawContact 以便它聚合到现有联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6524219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com