12

In my android application, I read out all the contacts with the following code:

ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
    while (cur.moveToNext()) {
        String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
        String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        ContentResolver bd = getContentResolver();
        String where = Data.RAW_CONTACT_ID+" = "+id+" and "+Data.MIMETYPE+" = "+CommonDataKinds.Event.CONTENT_ITEM_TYPE;
        Cursor bdc = bd.query(ContactsContract.Data.CONTENT_URI, null, where, null, null);
        if (bdc.getCount() > 0) {
            while (bdc.moveToNext()) {
                String birthday = bdc.getString(0);
                Toast.makeText(getApplicationContext(), id+name+birthday, Toast.LENGTH_SHORT);
            }
        }
    }
}
cur.close();

This is how I tried to read out the birthday event for every single contact. But obviously it doesn't work yet. So how can I read out the contact's date of birth correctly?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
caw
  • 30,999
  • 61
  • 181
  • 291

3 Answers3

29

Word of caution: some OEM's provide their own contact provider (not the standard Android one) and may not follow standard Android practices. For example, com.android.providers.contacts.HtcContactsProvider2 responds to queries on my HTC Desire HD

Here is one way:

// method to get name, contact id, and birthday
private Cursor getContactsBirthdays() {
    Uri uri = ContactsContract.Data.CONTENT_URI;

    String[] projection = new String[] {
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Event.CONTACT_ID,
            ContactsContract.CommonDataKinds.Event.START_DATE
    };

    String where =
            ContactsContract.Data.MIMETYPE + "= ? AND " +
            ContactsContract.CommonDataKinds.Event.TYPE + "=" + 
            ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
    String[] selectionArgs = new String[] { 
        ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE
    };
    String sortOrder = null;
    return managedQuery(uri, projection, where, selectionArgs, sortOrder);
}

// iterate through all Contact's Birthdays and print in log
Cursor cursor = getContactsBirthdays();
int bDayColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE);
while (cursor.moveToNext()) {
    String bDay = cursor.getString(bDayColumn);
    Log.d(TAG, "Birthday: " + bDay);
}

If this doesn't work, you may have an OEM modified contacts provider.

dstricks
  • 1,465
  • 12
  • 23
  • Thanks! You could also have linked this question ;) http://stackoverflow.com/questions/7376980/android-2-2-contact-birthday-date – caw Dec 30 '11 at 01:30
2

One can read out all the users' birthday with the following code:

ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
    while (cur.moveToNext()) {
        String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
        String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        ContentResolver bd = getContentResolver();
        Cursor bdc = bd.query(android.provider.ContactsContract.Data.CONTENT_URI, new String[] { Event.DATA }, android.provider.ContactsContract.Data.CONTACT_ID+" = "+id+" AND "+Data.MIMETYPE+" = '"+Event.CONTENT_ITEM_TYPE+"' AND "+Event.TYPE+" = "+Event.TYPE_BIRTHDAY, null, android.provider.ContactsContract.Data.DISPLAY_NAME);
        if (bdc.getCount() > 0) {
            while (bdc.moveToNext()) {
                String birthday = bdc.getString(0);
                // now "id" is the user's unique ID, "name" is his full name and "birthday" is the date and time of his birth
            }
        }
    }
}
cur.close();

But is there any better or shorter way to do it?

caw
  • 30,999
  • 61
  • 181
  • 291
1
ContentResolver cr = getContentResolver();
String where = Data.raw_contacts_id + " = your_id and " + Data.MIMETYPE + " = " +  CommonDataKinds.Events.CONTENT_ITEM_TYPE;
cr.query(ContactsContract.Data.CONTENT_URI, null, where, null, null);

I haven't test the code since i haven't install sdk in my computer. But i believe it should work.
Hope it will help you in some aspacts.

Frank Cheng
  • 5,928
  • 9
  • 52
  • 80
  • 1
    Thanks! I added your snippet do the code in my question. Is this how you meant it to be? – caw Dec 24 '11 at 03:06
  • Yes, I tried it. And unfortunately, it doesn't work (yet). It goes through the outer loop of contacts but only one time into the inner loop of birthdays (String birthday = bdc.getString(0)). And the contact for whom it enters the inner loop has no birthday set. – caw Dec 25 '11 at 02:58