1

I'm trying to add a birthday event to contacts but the event doesn't show up when displaying the contact in the People app. When going into 'Edit' mode of the contact, an event DOES show up, but no selected date (see attached screenshot) as if the format is wrong.

When inspecting the contacts2.db in an sqlite viewer, it seems that the date are formatted just fine, like other events/birthdays that are properly showing (see attached image. First row is a manually entered birthday through the app, and the second row, was added by my app and doesn't show)

Here is the code I am using, which follows the SampleSyncAdapter from the $ANDROID_HOME sdk

public ContactOperations addBirthday(Date date) {
    mValues.clear();
    if(date!=null) {
        mValues.put(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY);
        mValues.put(ContactsContract.CommonDataKinds.Event.START_DATE, BIRTHDATE_FORMATTER.format(date));
        mValues.put(ContactsContract.CommonDataKinds.Event.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE);
        addInsertOp();
    }
    return this;
}

private void addInsertOp() {

    if (!mIsNewContact) {
        mValues.put(ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID, mRawContactId);
    }
    ContentProviderOperation.Builder builder =
            newInsertCpo(ContactsContract.Data.CONTENT_URI, mIsSyncOperation, mIsYieldAllowed);
    builder.withValues(mValues);
    if (mIsNewContact) {
        builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, mBackReference);
    }
    mIsYieldAllowed = false;
    mBatchOperation.add(builder.build());
}

enter image description here

enter image description here

Alon Burg
  • 2,500
  • 2
  • 27
  • 32

1 Answers1

0

I`ve just had the same problem, and finally I've solved it. I wrote my code with the other way to do it, but the steps and result are the same.

I use your date format ("YYYY-MM-DD") and it works fine for me.

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

ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
        .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
        .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
        .build());

if (!myDate != null) {
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Event.DATA, myDate)
        .withValue(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY)
        .build());
         }

// Asking the Contact provider to create a new contact                 
try {
        getActivity().getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (Exception e) {
        e.printStackTrace();
    } 

I also change both "null's" for (althought with null's works too):

        .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.google")
        .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, --your google account--)

I hope to help you, sorry for my bad english.

Villapalos
  • 677
  • 9
  • 15