Rajesh Satvara
Rajesh Satvara

Reputation: 3964

Add app icon on Contact not working in Marshmallow

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 23 it creates new Contact with number.

in Api 21 Api 21

in Api 23 enter image description here

String 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();
                }

Upvotes: 0

Views: 420

Answers (1)

marmor
marmor

Reputation: 28189

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

EDIT

If you want to add this code to your existing batch:

ArrayList<ContentProviderOperation> ops = new ArrayList<>();
// insert account name and account type
ops.add(ContentProviderOperation.newInsert( ... ).build());
// insert contact number
ops.add(ContentProviderOperation.newInsert( ... ).build());
// insert mime-type data
ops.add(ContentProviderOperation.newInsert( ... ).build());

// add an AggregationExceptions line 
ops.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.

Upvotes: 3

Related Questions