Android: adding a birthday event to an android contact programmatically

2k views Asked by At

I am trying to birthday information to a contact. I use lookupkey to identify my contacts (as it is safer than just relying on contactId). In order to be able to insert the Event into the database i need a raw_contact_id ... so i'm trying to get this id:

String where = ContactsContract.Data.LOOKUP_KEY + " = ? AND "
            + ContactsContract.Data.MIMETYPE + " = ?";
String[] params = new String[] { lookupKey,
            ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE };

Cursor cursor = contentResolver.query(
            ContactsContract.Data.CONTENT_URI, null, where, params, null);
if (cursor.moveToFirst()) {
        birthdayRow = cursor.getInt(idIdx);
        long rawContactId = cursor.getLong(cursor
                .getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));
}

The problem is that if there is not birthday event set for a contact then this cursor i receive is empty ... and i don't know how to insert this event without a raw_contact_id. In order to insert the event i do the folowing:

values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
values.put(ContactsContract.Data.MIMETYPE, Event.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Event.START_DATE,
birthdayStartDate);
values.put(ContactsContract.CommonDataKinds.Event.TYPE,
            ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY);
values.put(ContactsContract.CommonDataKinds.Event.START_DATE,
            context.getString(R.string.birthday_label));
if (birthdayRow >= 0) {
        int result = contentResolver.update(
                ContactsContract.Data.CONTENT_URI, values,
                ContactsContract.Data._ID + " = " + birthdayRow, null);
        Log.i("ContactList", "update result: " + result);
} else {
        Uri result = contentResolver.insert(
                ContactsContract.Data.CONTENT_URI, values);
        Log.i("ContactList", "update result: " + result);
}

So please advice what shall i do, is there any way to add this event to the ContactData whitout a raw_contact id? Also i find strange the fact that for other ContactData like nickname i am doing the same thing and i dont get an empty cursor for the params String[] params = new String[] { String.valueOf(lookupKey), ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE }; even if the contact has no nickname.

1

There are 1 answers

0
Mark Pazon On BEST ANSWER

Use this to get the raw contact id before performing the insert.

    long rawContactId = -1; 
    String[] projection = new String[]{ContactsContract.CommonDataKinds.Event.RAW_CONTACT_ID}; 
    String selection = ContactsContract.CommonDataKinds.Event.CONTACT_ID + "=?";
    String[] selectionArgs = new String[]{
    String.valueOf(bdayContact.getId()) };
    Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI, projection, selection, selectionArgs, null); 
    try { 
        if (c.moveToFirst()) { 
             rawContactId = c.getLong(0); 
        } 
    } finally { 
        c.close(); 
    }