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 ();
}
}