获取生日的Andr​​oid应用程序的每个接触 [英] Get birthday for each contact in Android application

查看:180
本文介绍了获取生日的Andr​​oid应用程序的每个接触的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用程序中,我读出所有具有以下code联系人:

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?

在此先感谢!

推荐答案

词事项:一些OEM厂商提供自己的联系提供者(而不是标准的Andr​​oid的一个)和可以不遵循标准的Andr​​oid做法。例如,com.android.providers.contacts.HtcContactsProvider2响应查询我的HTC Desire HD

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

下面是单向的:

// 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);
}

如果这不起作用,你可能有一个OEM修改联系人提供商。

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

这篇关于获取生日的Andr​​oid应用程序的每个接触的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆