Android ContentProvider applyBatch 不保存所有联系人数据
Posted
技术标签:
【中文标题】Android ContentProvider applyBatch 不保存所有联系人数据【英文标题】:Android ContentProvider applyBatch not saving all contact data 【发布时间】:2013-05-22 10:56:35 【问题描述】:我正在尝试使用所有可用字段中的数据保存联系人。 但由于某种原因,并非所有数据都得到保存。 只有看起来有效的字段是:
结构化名称 电话 我 组织结构谁能告诉我我的错误在哪里?
public void saveContact(Context context)
ArrayList<ContentProviderOperation> op_list = new ArrayList<ContentProviderOperation>();
op_list.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, null)
.withValue(RawContacts.ACCOUNT_NAME, null)
.build());
op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, 0)
.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.DISPLAY_NAME, "Dislayname")
.withValue(StructuredName.PREFIX, "Prefix")
.withValue(StructuredName.GIVEN_NAME, "First")
.withValue(StructuredName.FAMILY_NAME, "Last")
.withValue(StructuredName.MIDDLE_NAME, "Middle")
.withValue(StructuredName.SUFFIX, "Suffix")
.build());
op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValue(Data.RAW_CONTACT_ID, 0)
.withValue(Data.MIMETYPE, Nickname.CONTENT_ITEM_TYPE)
.withValue(Nickname.NAME, "Nickname")
.withValue(Nickname.TYPE, Nickname.TYPE_SHORT_NAME)
.build());
op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, 0)
.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.TYPE, Phone.TYPE_WORK)
.withValue(Phone.NUMBER, "66666666")
.withValue(Phone.LABEL, "")
.build());
op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValue(Data.RAW_CONTACT_ID, 0)
.withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
.withValue(Email.TYPE, Email.TYPE_WORK)
.withValue(Email.DATA, "test@test.com")
.build());
op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValue(Data.RAW_CONTACT_ID, 0)
.withValue(Data.MIMETYPE, Website.CONTENT_ITEM_TYPE)
.withValue(Website.TYPE, Website.TYPE_WORK)
.withValue(Website.URL, "www.site.com")
.build());
op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValue(Data.RAW_CONTACT_ID, 0)
.withValue(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE)
.withValue(StructuredPostal.TYPE, StructuredPostal.TYPE_HOME)
.withValue(StructuredPostal.STREET, "TEST STREET")
.withValue(StructuredPostal.CITY, "TEST CITY")
.withValue(StructuredPostal.REGION, "TEST REGION")
.withValue(StructuredPostal.POSTCODE, "00000")
.withValue(StructuredPostal.COUNTRY, "TEST COUNTRY")
.withValue(StructuredPostal.NEIGHBORHOOD, "TEST NEIGHBORHOOD")
.withValue(StructuredPostal.POBOX, "TEST POBOX")
.build());
op_list.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, 0)
.withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE )
.withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, "Company")
.withValue(ContactsContract.CommonDataKinds.Organization.TITLE, "Job Title")
.withValue(ContactsContract.CommonDataKinds.Organization.JOB_DESCRIPTION, "Job description/position")
.withValue(ContactsContract.CommonDataKinds.Organization.DEPARTMENT, "Department")
.withValue(ContactsContract.CommonDataKinds.Organization.OFFICE_LOCATION, "Office location")
.withValue(ContactsContract.CommonDataKinds.Organization.SYMBOL, "Symbol")
.withValue(ContactsContract.CommonDataKinds.Organization.PHONETIC_NAME, "Phonetic name")
.withValue(ContactsContract.CommonDataKinds.Organization.TYPE, ContactsContract.CommonDataKinds.Organization.TYPE_WORK)
.build());
op_list.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, 0)
.withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Im.DATA, "skype@a.com")
.withValue(ContactsContract.CommonDataKinds.Im.TYPE, Im.TYPE_WORK)
.withValue(ContactsContract.CommonDataKinds.Im.PROTOCOL, Im.PROTOCOL_SKYPE)
.build());
op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValue(Data.RAW_CONTACT_ID, 0)
.withValue(Data.MIMETYPE, Nickname.CONTENT_ITEM_TYPE)
.withValue(Note.NOTE, "Note")
.build());
op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValue(Data.RAW_CONTACT_ID, 0)
.withValue(Data.MIMETYPE, android.provider.ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE)
.withValue(android.provider.ContactsContract.CommonDataKinds.Event.START_DATE, "2013-06-06")
.withValue(android.provider.ContactsContract.CommonDataKinds.Event.TYPE, android.provider.ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY)
.withValue(android.provider.ContactsContract.CommonDataKinds.Event.LABEL, "Birthday")
.build());
op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValue(Data.RAW_CONTACT_ID, 0)
.withValue(Data.MIMETYPE, Relation.CONTENT_ITEM_TYPE)
.withValue(Relation.NAME, "Relation name")
.withValue(Relation.TYPE, Relation.TYPE_BROTHER)
.build());
try
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, op_list);
catch(Exception e)
Log.d("contacts", "saveContact", e);
【问题讨论】:
【参考方案1】:这实际上只是一个很小但很重要的错字!您仅将withValueBackreference()
用于那些起作用的类型 - 您将withValue()
用于其他类型。难怪,那些不起作用。只需在这里也使用withValueBackReference()
,一切都会按预期工作!
【讨论】:
啊,我不敢相信我错过了.. doh!非常感谢! :) 不要求爱。像这样的事情一直在发生。而且它也花费了我们太多时间来找到它:-)【参考方案2】:有一个关于withValueBackreference
和withValue
的impeccable explanation。
【讨论】:
以上是关于Android ContentProvider applyBatch 不保存所有联系人数据的主要内容,如果未能解决你的问题,请参考以下文章
Android19.3 ContentProvider及安卓进一步封装后的相关类