您缺少请将此新的原始联系人附加到此现有联系人中"部分.为此,您需要添加 AggregationExceptions首先,在要添加的Contact中找到当前的RawContact IDs,然后在AggregationExceptions中添加一行,以链接新的RawContact._ID(raw1)和现有的RawContact._ID( raw2)Builder builder = ContentProviderOperation.newUpdate(AggregationExceptions.CONTENT_URI);builder.withValue(AggregationExceptions.TYPE, AggregationExceptions.TYPE_KEEP_TOGETHER);builder.withValue(AggregationExceptions.RAW_CONTACT_ID1, raw1);builder.withValue(AggregationExceptions.RAW_CONTACT_ID2, raw2);ops.add(builder.build()); 编辑如果要将此代码添加到现有批次中:ArrayList<ContentProviderOperation> ops = new ArrayList<>();// insert account name and account typeops.add(ContentProviderOperation.newInsert( ... ).build());// insert contact numberops.add(ContentProviderOperation.newInsert( ... ).build());// insert mime-type dataops.add(ContentProviderOperation.newInsert( ... ).build());// add an AggregationExceptions lineops.add(ContentProviderOperation.newUpdate(AggregationExceptions.CONTENT_URI) .withValue(AggregationExceptions.TYPE, AggregationExceptions.TYPE_KEEP_TOGETHER) .withValueBackReference(AggregationExceptions.RAW_CONTACT_ID1, 0) .withValue(AggregationExceptions.RAW_CONTACT_ID2, theRawContactIdOfTheExistingContact) .build());try { resolver.applyBatch(ContactsContract.AUTHORITY, ops);} catch (Exception e) { ... }您唯一需要填写的是theRawContactIdOfTheExistingContact,请注意,它不是不是联系人ID,而是 raw-contact-id ,您将需要在其中放置正确的值,具体取决于代码的其余部分以及如何找到要向其中添加数据的联系人.I am adding icon of my app in phonebook. now the problem is that its working fine in Api level < 23 but not working on Api level > 23.in Api 21in Api 23String MIMETYPE = "vnd.android.cursor.item/com.appiconincontact"; ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); // insert account name and account type ops.add( ContentProviderOperation .newInsert(addCallerIsSyncAdapterParameter(RawContacts.CONTENT_URI, true)) .withValue(RawContacts.ACCOUNT_NAME, Constants.ACCOUNT_NAME) .withValue(RawContacts.ACCOUNT_TYPE, Constants.ACCOUNT_TYPE) .withValue(RawContacts.AGGREGATION_MODE, RawContacts.AGGREGATION_MODE_DEFAULT) .build() ); // insert contact number ops.add(ContentProviderOperation .newInsert(addCallerIsSyncAdapterParameter(ContactsContract.Data.CONTENT_URI, true)) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number) .build()); // insert mime-type data ops.add(ContentProviderOperation .newInsert(addCallerIsSyncAdapterParameter(ContactsContract.Data.CONTENT_URI, true)) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, MIMETYPE) .withValue(ContactsContract.Data.DATA2, Constants.APP_NAME) .withValue(ContactsContract.Data.DATA3, "User Connected with " + number) .build()); try { resolver.applyBatch(ContactsContract.AUTHORITY, ops); } catch (Exception e) { e.printStackTrace(); } 解决方案 You're creating a new RawContact, and hoping that the system aggregates it into an existing Contact.You're missing the "please attach this new raw-contact into this existing contact" part.To do that you need to add an AggregationExceptions.First, find the current RawContact IDs in the Contact you wish to add to, then add a line to AggregationExceptions that links between your new RawContact._ID (raw1) and an existing RawContact._ID (raw2)Builder builder = ContentProviderOperation.newUpdate(AggregationExceptions.CONTENT_URI);builder.withValue(AggregationExceptions.TYPE, AggregationExceptions.TYPE_KEEP_TOGETHER);builder.withValue(AggregationExceptions.RAW_CONTACT_ID1, raw1);builder.withValue(AggregationExceptions.RAW_CONTACT_ID2, raw2);ops.add(builder.build());EDITIf you want to add this code to your existing batch:ArrayList<ContentProviderOperation> ops = new ArrayList<>();// insert account name and account typeops.add(ContentProviderOperation.newInsert( ... ).build());// insert contact numberops.add(ContentProviderOperation.newInsert( ... ).build());// insert mime-type dataops.add(ContentProviderOperation.newInsert( ... ).build());// add an AggregationExceptions lineops.add(ContentProviderOperation.newUpdate(AggregationExceptions.CONTENT_URI) .withValue(AggregationExceptions.TYPE, AggregationExceptions.TYPE_KEEP_TOGETHER) .withValueBackReference(AggregationExceptions.RAW_CONTACT_ID1, 0) .withValue(AggregationExceptions.RAW_CONTACT_ID2, theRawContactIdOfTheExistingContact) .build());try { resolver.applyBatch(ContactsContract.AUTHORITY, ops);} catch (Exception e) { ... }The only thing you need to fill in here is theRawContactIdOfTheExistingContact, note that it's not a contact-id, it's a raw-contact-id, you'll need to put the right value there, depending on the rest of your code, and how you find the contact to add your data to. 这篇关于在无法在棉花糖中使用的联系人上添加应用程序图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-07 23:07