Android的ContentProvider的applyBatch不保存所有的联系人数据 [英] Android ContentProvider applyBatch not saving all contact data

查看:1064
本文介绍了Android的ContentProvider的applyBatch不保存所有的联系人数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去保存所有可用字段中的数据的联系人。
但由于某些原因,不是所有的数据被保存。
只有那些似乎工作领域是:


  • StructuredName

  • 电话

  • 即时消息

  • 组织

谁能给我在我的错误在于一个想法?

 公共无效saveContact(上下文的背景下)
    {
        ArrayList的< ContentProviderOperation> op_list =新的ArrayList< ContentProviderOperation>();        op_list.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
                .withValue(RawContacts.ACCOUNT_TYPE,NULL)
                .withValue(RawContacts.ACCOUNT_NAME,NULL)
                。建立());        op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
        .withValueBackReference(Data.RAW_CONTACT_ID,0)
        .withValue(Data.MIMETYPE,StructuredName.CONTENT_ITEM_TYPE)
        .withValue(StructuredName.DISPLAY_NAMEDislayname)
        .withValue(StructuredName。preFIX,preFIX)
        .withValue(StructuredName.GIVEN_NAME,第一)
        .withValue(StructuredName.FAMILY_NAME,上次)
        .withValue(StructuredName.MIDDLE_NAME,中)
        .withValue(StructuredName.SUFFIX,后缀)
        。建立());        op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                  .withValue(Data.RAW_CONTACT_ID,0)
                  .withValue(Data.MIMETYPE,Nickname.CONTENT_ITEM_TYPE)
                  .withValue(Nickname.NAME,昵称)
                  .withValue(Nickname.TYPE,Nickname.TYPE_SHORT_NAME)
                  。建立());        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,)
                 。建立());        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)
                 。建立());        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.URLwww.site.com)
                 。建立());        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街)
                .withValue(StructuredPostal.CITY,TEST CITY)
                .withValue(StructuredPostal.REGION,TEST区)
                .withValue(StructuredPostal.POST code,00000)
                .withValue(StructuredPostal.COUNTRY,TEST国家)
                .withValue(StructuredPostal.NEIGHBORHOOD,TEST邻居)
                .withValue(StructuredPostal.POBOX,TEST POBOX)
                。建立());        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,本公司)
                .withValue(ContactsContract.CommonDataKinds.Organization.TITLE,职位)
                .withValue(ContactsContract.CommonDataKinds.Organization.JOB_DESCRIPTION,职位描述/位置)
                .withValue(ContactsContract.CommonDataKinds.Organization.DEPARTMENT,部门)
                .withValue(ContactsContract.CommonDataKinds.Organization.OFFICE_LOCATION,办公地点)
                .withValue(ContactsContract.CommonDataKinds.Organization.SYMBOL,符号)
                .withValue(ContactsContract.CommonDataKinds.Organization.PHONETIC_NAME,拼音名称)
                .withValue(ContactsContract.CommonDataKinds.Organization.TYPE,ContactsContract.CommonDataKinds.Organization.TYPE_WORK)
                。建立());        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.DATAskype@a.com)
                  .withValue(ContactsContract.CommonDataKinds.Im.TYPE,Im.TYPE_WORK)
                  .withValue(ContactsContract.CommonDataKinds.Im.PROTOCOL,Im.PROTOCOL_SKYPE)
                  。建立());        op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                  .withValue(Data.RAW_CONTACT_ID,0)
                  .withValue(Data.MIMETYPE,Nickname.CONTENT_ITEM_TYPE)
                  .withValue(Note.NOTE,注)
                  。建立());        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年6月6日)
                  .withValue(android.provider.ContactsContract.CommonDataKinds.Event.TYPE,android.provider.ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY)
                  .withValue(android.provider.ContactsContract.CommonDataKinds.Event.LABEL,生日)
                  。建立());        op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                  .withValue(Data.RAW_CONTACT_ID,0)
                  .withValue(Data.MIMETYPE,Relation.CONTENT_ITEM_TYPE)
                  .withValue(Relation.NAME,关系名称)
                  .withValue(Relation.TYPE,Relation.TYPE_BROTHER)
                  。建立());        尝试{
            。context.getContentResolver()applyBatch(ContactsContract.AUTHORITY,op_list);
        }赶上(例外五){
            Log.d(联系人,saveContact,E);
        }
    }


解决方案

这其实只是一个很小但很重要的错字!您可以使用 withValueBackreference()只为那些函数类型 - 你使用 withValue()为他人。也难怪,这些不工作。只需使用 withValueBackReference()在这里也和所有的工作将按照预期!

Im trying to save a contact with data in all available fields. But for some reason not all the data gets saved. Only fields that seem to work are:

  • StructuredName
  • Phone
  • Im
  • Organization

Can anyone give me an idea of where my mistake lies?

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

解决方案

It's actually just a tiny but important typo! You use withValueBackreference() only for those types which function - you use withValue() for those others. No wonder, those do not work. Just use withValueBackReference() here as well and all will work as expected!

这篇关于Android的ContentProvider的applyBatch不保存所有的联系人数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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