【问题标题】:Getting contact's birthday error获取联系人的生日错误
【发布时间】:2014-03-16 07:48:00
【问题描述】:

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

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

虚拟设备------------

显示黑屏和我的应用程序停止

LOGCAT-----

Log/I-Start reading birthdays from contacts

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

最好的问候,SergaRUS

【问题讨论】:

    标签: android contacts


    【解决方案1】:

    参考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);
    }
    

    【讨论】:

    • 在一些日志检查后抱歉,错误在return managedQuery(uri, projection, where, selectionArgs, sortOrder);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多