添加ContactsContract.CommonDataKinds.Event到Android通讯录,显示不出来 [英] Adding a ContactsContract.CommonDataKinds.Event to Android contacts, does not show up

查看:719
本文介绍了添加ContactsContract.CommonDataKinds.Event到Android通讯录,显示不出来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加一个生日事件接触,但该事件的显示不出来显示在联系人应用的联系人时。当进入联系人的编辑模式下,事件不会显示出来,但是没有指定日期(见附件截图),如果格式是错误的。

I'm trying to add a birthday event to contacts but the event doesn't show up when displaying the contact in the People app. When going into 'Edit' mode of the contact, an event DOES show up, but no selected date (see attached screenshot) as if the format is wrong.

当在源码观察者检查contacts2.db,似乎日期被格式化
就好了,像其他事件/生日被正确显示(见附表形象。第一排是通过应用程序手动输入的生日,第二行,是由我的应用程序添加并没有显示)

When inspecting the contacts2.db in an sqlite viewer, it seems that the date are formatted just fine, like other events/birthdays that are properly showing (see attached image. First row is a manually entered birthday through the app, and the second row, was added by my app and doesn't show)

下面是code我使用,从$ ANDROID_HOME SDK它遵循SampleSyncAdapter

Here is the code I am using, which follows the SampleSyncAdapter from the $ANDROID_HOME sdk

public ContactOperations addBirthday(Date date) {
    mValues.clear();
    if(date!=null) {
        mValues.put(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY);
        mValues.put(ContactsContract.CommonDataKinds.Event.START_DATE, BIRTHDATE_FORMATTER.format(date));
        mValues.put(ContactsContract.CommonDataKinds.Event.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE);
        addInsertOp();
    }
    return this;
}

private void addInsertOp() {

    if (!mIsNewContact) {
        mValues.put(ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID, mRawContactId);
    }
    ContentProviderOperation.Builder builder =
            newInsertCpo(ContactsContract.Data.CONTENT_URI, mIsSyncOperation, mIsYieldAllowed);
    builder.withValues(mValues);
    if (mIsNewContact) {
        builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, mBackReference);
    }
    mIsYieldAllowed = false;
    mBatchOperation.add(builder.build());
}

推荐答案

I`ve刚刚有同样的问题,最后我已经解决了。
我写我的code用其他的方式来做到这一点,但步骤和结果都是一样的。

I`ve just had the same problem, and finally I've solved it. I wrote my code with the other way to do it, but the steps and result are the same.

我用你的日期格式(YYYY-MM-DD),它为我工作得很好。

I use your date format ("YYYY-MM-DD") and it works fine for me.

ArrayList <ContentProviderOperation> ops = new ArrayList < ContentProviderOperation > ();

ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
        .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
        .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
        .build());

if (!myDate != null) {
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Event.DATA, myDate)
        .withValue(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY)
        .build());
         }

// Asking the Contact provider to create a new contact                 
try {
        getActivity().getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (Exception e) {
        e.printStackTrace();
    } 

我也同时改变空的的(虽然用空的作品太):

I also change both "null's" for (althought with null's works too):

        .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.google")
        .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, --your google account--)

我希望能帮助你,对不起我的英文不好。

I hope to help you, sorry for my bad english.

这篇关于添加ContactsContract.CommonDataKinds.Event到Android通讯录,显示不出来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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