Get birthday for each contact in Android application

前端 未结 3 458
予麋鹿
予麋鹿 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: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?

提交回复
热议问题