Add multiple contacts in batch mode

I can perfectly add contacts one by one with the following code:

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI) .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null) .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).build()); ops.add(ContentProviderOperation .newInsert(Data.CONTENT_URI) .withValueBackReference(Data.RAW_CONTACT_ID, 0) .withValue(Data.MIMETYPE, CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) .withValue(StructuredName.GIVEN_NAME, "Hello") .withValue(StructuredName.FAMILY_NAME, "World").build()); try { getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (OperationApplicationException e) { // TODO Auto-generated catch block e.printStackTrace(); } 

However, when I try to add about 500 contacts one by one, it takes a few minutes, which is too long for my application. Is there a faster way to add multiple contacts?

+6
source share
2 answers

Why not make the arraylist global, which can be accessed from any activity that I wouldn’t insert so much into the Bundle, since it happens more when you do this, it was intended to transmit small amounts of information. I would do it this way, making sure you also name it in the manifest.

 public class MyStates extends Application { private ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); public ArrayList getList() { return this.blueToothAdapter; } public void setList(ArrayList<ContentProviderOperation> o) { this.ops= o; } 
0
source

You can use the same function that you use to add several contacts in one batch operation, making small changes.

You can add up to 500 operations for one batch operation, you can continue to include the backlink in the Data Uri operations with the corresponding index of the raw_contacts insert operation.

0
source

Source: https://habr.com/ru/post/895423/


All Articles