Contact API storing contact as an invisible contact: How to make it visible?

旧城冷巷雨未停 提交于 2019-11-27 14:06:09

问题


I am trying to add a contact in Android using getContentResolver. First I created an ArrayList:

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

then populated the array list by

int rawContactInsertIndex = ops.size();

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

ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
   .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,rawContactInsertIndex)
   .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
   .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name)
   .build());

and finally in a try block

getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

when I excecute this, I am not getting any error or exception. But the contact does not appear in the Android contacts. When I retrieve the invisible contacts I could find this contact. Can any one figure out what is going wrong?


回答1:


 ArrayList<ContentProviderOperation> op_list = new ArrayList<ContentProviderOperation>();
 op_list.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
        .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
        .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
      //.withValue(RawContacts.AGGREGATION_MODE, RawContacts.AGGREGATION_MODE_DEFAULT)
        .build()); 

     // first and last names
 op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
        .withValueBackReference(Data.RAW_CONTACT_ID, 0)
        .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
        .withValue(StructuredName.GIVEN_NAME, name)
        .withValue(StructuredName.FAMILY_NAME, name)
        .build());

 op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
        .withValueBackReference(Data.RAW_CONTACT_ID, 0)
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phone)
        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,  Phone.TYPE_MOBILE)
        .build());

  op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
         .withValueBackReference(Data.RAW_CONTACT_ID, 0)
       .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
         .withValue(ContactsContract.CommonDataKinds.Email.DATA, email)
         .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK)
         .build());

try{
     ContentProviderResult[] results = getContentResolver().
                                 applyBatch(ContactsContract.AUTHORITY, op_list);
}catch(Exception e){
     e.printStackTrace();
} 

this code works!



来源:https://stackoverflow.com/questions/4387960/contact-api-storing-contact-as-an-invisible-contact-how-to-make-it-visible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!