Get birthday for each contact in Android application

前端 未结 3 479
予麋鹿
予麋鹿 2020-12-08 17:08

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

ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsCont         


        
3条回答
  •  臣服心动
    2020-12-08 17:48

    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.

提交回复
热议问题