Get birthday for each contact in Android application

房东的猫 提交于 2019-12-17 18:14:28

问题


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?


回答1:


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.




回答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?




回答3:


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.



来源:https://stackoverflow.com/questions/8579883/get-birthday-for-each-contact-in-android-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!