Get birthday for each contact in Android application

前端 未结 3 453
予麋鹿
予麋鹿 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:31
    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.

    0 讨论(0)
  • 2020-12-08 17:42

    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?

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题