2010-07-12 88 views
3

我正在嘗試在Android 2.1中爲聯繫人數據庫寫入新聯繫人。不過,我收到以下錯誤:聯繫人寫入聯繫人數據庫 - 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); 
} 

回答

2
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rowContactInsertIndex).withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE).withValue(Phone.NUMBER, cPhone).withValue(Phone.TYPE, cPhoneType).build()); 

這可能幫助您。對全名,電話和電子郵件也一樣