Inserting contacts in Android 2.2

后端 未结 3 586
后悔当初
后悔当初 2020-11-27 16:01

I am trying to insert new RawContact contacts, but the RawContact added doesn\'t get displayed when I view the contacts through Contacts or phonebo

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-27 16:37

    I thought this Q was long forgotten but Since someone upvoted it, I am assuming someone else also faces the same problem as me. After a little struggle I was able to figure out the problem and insert contacts, Hope this helps, here is the sample code:

    ArrayList ops = new ArrayList();
    int rawContactInsertIndex = ops.size();
    
    ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
       .withValue(RawContacts.ACCOUNT_TYPE, null)
       .withValue(RawContacts.ACCOUNT_NAME,null )
       .build());
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
       .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
       .withValue(Data.MIMETYPE,Phone.CONTENT_ITEM_TYPE)
       .withValue(Phone.NUMBER, "9X-XXXXXXXXX")
       .build());
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
       .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
       .withValue(Data.MIMETYPE,StructuredName.CONTENT_ITEM_TYPE)
       .withValue(StructuredName.DISPLAY_NAME, "Mike Sullivan")
       .build());  
    ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    

提交回复
热议问题