Question

Im trying to add a phone number to an already existing contact on a Droid-phone. Doing it at the same time as I create a contact is trivial, as the backreference I supply simply is 0 when creating a ContentProviderOperation. But trying to find the backreference through querying for a display name like this does not work:

Cursor rawContactsReferenceCursor = 
            contentResolver.query(Data.CONTENT_URI, 
                    new String[]{Data.RAW_CONTACT_ID},
                    Data.DISPLAY_NAME+"=\""+displayName+"\"", null, null);

While I do get a raw contact ID, the following code just generates an IndexOutOfBoundException (rawConcactReferenceID is the variable I got from the previous query):

ArrayList<ContentProviderOperation> op_list = 
    new ArrayList<ContentProviderOperation>();
op_list.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
    .withValueBackReference(Data.RAW_CONTACT_ID, rawConcactReferenceID)
    .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
    .withValue(Phone.NUMBER, testNumber)
    .withValue(Phone.TYPE, Phone.TYPE_CUSTOM)
    .withValue(Phone.LABEL, testLabel)
    .build()); 
ContentProviderResult[] result =
    contentResolver.applyBatch(ContactsContract.AUTHORITY, op_list);

The big challenge is a huge lack of good documentation. I would be very satisfied to just get my hands on some working copypasta to study.

Cheers,

Was it helpful?

Solution

I found an answer. It is not atomic if you want to add several things right away, but hey, who needs stupid atomicity?

ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, new Integer(contactId).intValue());
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, dataValue);
values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
values.put(Phone.LABEL, customLabel);
Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values);

OTHER TIPS

I've had a similar problem with email addresses. Here's the solution I used that worked:

ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
    .withValue(Data.RAW_CONTACT_ID, id)
    .withValue(Email.DATA, value)
    .withValue(Email.MIMETYPE, .Email.CONTENT_ITEM_TYPE)
    .withValue(Email.LABEL, label)
    .withValue(Email.TYPE, Email.TYPE_CUSTOM)
    .build());
ContentProviderResult[] res = cr.applyBatch(ContactsContract.AUTHORITY, ops);

The same solution should work for telephone numbers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top