收到联系人的生日错误

时间:2014-03-16 07:48:01

标签: android contacts

我需要联系生日,但我的代码不起作用

CODE.java ----------------

Log.i(TAG, "Start reading birthdays from contacts");
    Uri uri = ContactsContract.Data.CONTENT_URI;

    Log.d(TAG, "1");
    String[] projection = new String[] {
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Event.CONTACT_ID,
                ContactsContract.CommonDataKinds.Event.START_DATE
    };

    Log.d(TAG, "2");
    String where = ContactsContract.Data.MIMETYPE + "= ? AND " +
            ContactsContract.CommonDataKinds.Event.TYPE + "=" +
            ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;

    Log.d(TAG, "3");
    String[] selectionArgs = new String[] {ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE};

    Log.d(TAG, "4");
    Cursor cursor = managedQuery(uri, projection, where, selectionArgs, null);


    while (cursor.moveToNext()) {
        Log.d(TAG, "5");
        String displayBirthday = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

        if(!displayBirthday.equals("")){
            Log.d(TAG, "6");
            tvCon.setText(name + " - " + displayBirthday + "\n");
        }
    }

VIRTUAL DEVICE ------------

显示黑屏和我的app制动器

的logcat -----------------

Log/I-Start reading birthdays from contacts

所以问题在于创建uri,但它看起来像rite)

最好的问候,SergaRUS

1 个答案:

答案 0 :(得分:0)

请参阅https://stackoverflow.com/a/8638744/2771869尝试使用:

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