联系API存储联系人作为隐形联系人:如何使其可见?

时间:2021-11-11 11:19:03

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

我正在尝试使用getContentResolver在Android中添加联系人。首先我创建了一个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?

当我执行此操作时,我没有收到任何错误或异常。但该联系人不会出现在Android联系人中。当我找回隐形联系人时,我可以找到这个联系人。谁能弄清楚出了什么问题?

1 个解决方案

#1


10  

 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!

这段代码有效!

#1


10  

 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!

这段代码有效!