How to add Contacts in batches

Source: Internet
Author: User

To be honest, I have been searching for all the posts throughout the day. It seems that all the posts about batch add Contacts are copied by one person!

 

Then the man did not make it clear.

 

Which of the following is what I did! Implement the function. I feel

 

On the API, there are all ready-made items in the sample, but batch processing is only for a specific contact!

 

As a result, I don't know if I want to cyclically Add a contact to the ops to use applybatch together.

 

Fortunately, the conjecture is not too outrageous! Run the Code directly.

/**

* The list stores the list of contacts I want to add in batches.

* The example withyieldallowed (true) is not written in other places. After reading the official documentation, you can see that batch operations are easy to occupy the database for a long time.

* Therefore, you need to write this credit point to ensure that other programs that want to read the database will interrupt this operation. If this parameter is not added, an error will occur!

* Rawcontactinsertindex = ops. Size (); at first, I thought it was confusing to replace the result address book with cyclic I!

*/

 

Public static void copyall2phone (list <contactinfo> list, context CTX ){

Arraylist <contentprovideroperation> Ops = new arraylist <contentprovideroperation> ();

Int rawcontactinsertindex;

For (INT I = 0; I <list. Size (); I ++ ){

Rawcontactinsertindex = ops. Size (); // This sentence is very important. With this sentence, you can add the real implementation in batches.

 

Ops. Add (contentprovideroperation. newinsert (contactscontract. rawcontacts. content_uri)

. Withvalue (contactscontract. rawcontacts. account_type, null)

. Withvalue (contactscontract. rawcontacts. account_name, null)

. Withyieldallowed (true)

. 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, list. Get (I). Name)

. Withyieldallowed (true)

. Build ());

Ops. Add (contentprovideroperation. newinsert (contactscontract. Data. content_uri)

. Withvaluebackreference (contactscontract. Data. raw_contact_id, rawcontactinsertindex)

. Withvalue (contactscontract. Data. mimetype,

Contactscontract. commondatakinds. Phone. content_item_type)

. Withvalue (contactscontract. commondatakinds. Phone. Number, list. Get (I). Number)

. Withvalue (contactscontract. commondatakinds. Phone. type, phone. type_mobile)

. Withyieldallowed (true)

. Build ());

 

}

Try {

// Batch add called only here

CTX. 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 ();

}

 

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.