Android入门教程:增删改查通讯录(2)

public void testAddContactsInTransaction() throws Exception {           Uri uri = Uri.parse("content://com.Android.contacts/raw_contacts");           ContentResolver resolver = this.getContext().getContentResolver();           ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();           // 向raw_contact表添加一条记录            //此处.withValue("account_name", null)一定要加,不然会抛NullPointerException            ContentProviderOperation operation1 = ContentProviderOperation                   .newInsert(uri).withValue("account_name"null).build();           operations.add(operation1);           // 向data添加数据            uri = Uri.parse("content://com.android.contacts/data");           //添加姓名            ContentProviderOperation operation2 = ContentProviderOperation                   .newInsert(uri).withValueBackReference("raw_contact_id"0)                   //withValueBackReference的第二个参数表示引用operations[0]的操作的返回id作为此值                    .withValue("mimetype""vnd.android.cursor.item/name")                   .withValue("data2""xzdong").build();           operations.add(operation2);           //添加手机数据            ContentProviderOperation operation3 = ContentProviderOperation                   .newInsert(uri).withValueBackReference("raw_contact_id"0)                   .withValue("mimetype""vnd.android.cursor.item/phone_v2")                   .withValue("data2""2").withValue("data1""0000000").build();           operations.add(operation3);           resolver.applyBatch("com.android.contacts", operations);       }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/pxsxz.html