我正在尝试将新联系人写入Android 2.1的联系人数据库。但是,我遇到了以下错误:
java.lang.UnsupportedOperationException: Aggregate contacts are created automatically.
我已在清单中添加了以下字段:
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
这是我的代码:
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
String accountType = null;
String accountName = null;
Builder builder = ContentProviderOperation.newInsert(
ContactsContract.Contacts.CONTENT_URI);
builder.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType);
builder.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accountName);
ContentProviderOperation op = builder.build();
Log.i("contacts", "op.getUri(): " + op.getUri());
ops.add(op);
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID,
ops.size()
)
.withValue(
ContactsContract.Data.MIMETYPE,
StructuredName.CONTENT_ITEM_TYPE
)
.withValue(StructuredName.DISPLAY_NAME, contact.getFullName())
.build()
);
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID,
ops.size()
)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE
)
.withValue(
ContactsContract.CommonDataKinds.Phone.NUMBER,
contact.getPhoneNumber()
)
.build()
);
try {
this.cr.applyBatch(ContactsContract.AUTHORITY, ops);
}