6

I am perfectly able to add contacts one by one with 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 few minutes, which is too long for my app. Is the any faster way to add several contacts?

lstipakov
  • 3,138
  • 5
  • 31
  • 46
  • 1
    You could add them in a Thread - not the UI Thread. – Phil Aug 19 '11 at 14:55
  • I do it in separate thread, the problem is that operation itself takes too much time. In iPhone it takes few seconds for 500 contacts. – lstipakov Aug 19 '11 at 15:00
  • I do it in a similar way, however I do the batches for every time I want to add a contact. I don't think it's the right way, but it seems to work okay – Joe Simpson Aug 19 '11 at 15:07
  • Well, if iPhone do it for several seconds but Android for few minutes, I am inclined to think that I am using the wrong API. – lstipakov Aug 19 '11 at 15:10
  • Quit using ApplyBatch() and start using [BulkInsert()][1]. [1]: http://stackoverflow.com/questions/5596354/insertion-of-thousands-of-contact-entries-using-applybatch-is-slow/5597497#5597497 – jcwenger Sep 08 '13 at 03:13

2 Answers2

0

Why not make the arraylist a global that can be accessed from any activity I wouldn't insert that much into a Bundle as there more going on when you do, it was only meant to pass small amounts info. I would do it like this, making sure to call this in the manifest too..

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;
    }
JPM
  • 9,077
  • 13
  • 78
  • 137
  • How this is related to my question? – lstipakov Sep 12 '11 at 11:01
  • It is a legit answer instead of using the Bundle to pass enormous amounts of data you create a global array that is accessible to all activities. – JPM Sep 12 '11 at 16:35
  • I don't have a problem passing data between activities. I have a problem with slow Contacts API. – lstipakov Sep 13 '11 at 13:49
  • Yes I know but you are trying to do so much in one step...how about isolating what is slow? I was thinking that maybe the large bundle creation and passing to each activity was slow hence, the global creation of an array. If that is not it then before passing the bundle, thread the retrieving of the contacts in a background process. – JPM Sep 13 '11 at 14:24
0

You can use the same function you are using to add multiple contacts in a single batch operation by making small modifications.

You can add upto 500 operations to a single batch operation, you can keep on including the back-reference in Data Uri operation with the corresponding index of the raw_contacts insert operation.

Ankur Kumar
  • 972
  • 7
  • 12
  • 1
    Could you expand on this answer, what small modifications? I am having a similar issue, however looping through the code seems to cause (unknown) contacts being added. [link](http://stackoverflow.com/questions/11886900/inserting-multiple-contacts-in-android-2-3-5) – David Hirst Aug 15 '12 at 15:29