2. 使用ContentResolver的applyBatch(String authority,ArrayList<ContentProviderOperation> operations) 方法可以将多个操作在一个事务中执行
3. 文档位置:
file:///F:/android-sdk-windows/docs/reference/android/provider/ContactsContract.RawContacts.html
示例:
//使用事务添加联系人
public void testInsertBatch() throws Exception {
ContentResolver resolver = getContext().getContentResolver();
ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
ContentProviderOperation operation1 = ContentProviderOperation //
.newInsert(Uri.parse("content://com.android.contacts/raw_contacts")) //
.withValue("_id", null) //
.build();
operations.add(operation1);
ContentProviderOperation operation2 = ContentProviderOperation //
.newInsert(Uri.parse("content://com.android.contacts/data")) //
.withValueBackReference("raw_contact_id", 0) //
.withValue("data2", "ZZH") //
.withValue("mimetype", "vnd.android.cursor.item/name") //
.build();
operations.add(operation2);
ContentProviderOperation operation3 = ContentProviderOperation //
.newInsert(Uri.parse("content://com.android.contacts/data")) //
.withValueBackReference("raw_contact_id", 0) //
.withValue("data1", "18612312312") //
.withValue("data2", "2") //
.withValue("mimetype", "vnd.android.cursor.item/phone_v2") //
.build();
operations.add(operation3);
ContentProviderOperation operation4 = ContentProviderOperation //
.newInsert(Uri.parse("content://com.android.contacts/data")) //
.withValueBackReference("raw_contact_id", 0) //
.withValue("data1", "zq@itcast.cn") //
.withValue("data2", "2") //
.withValue("mimetype", "vnd.android.cursor.item/email_v2") //
.build();
operations.add(operation4);
// 在事务中对多个操作批量执行
resolver.applyBatch("com.android.contacts", operations);
}
希望本文所述对大家Android程序设计有所帮助。
注:相关教程知识阅读请移步到Android开发频道。










