我正在使用此代码将文件中的联系人存储到手机的联系人列表中:
public void addContacts(String name, String number, String type) {
int backRefIndex = 0;
String data=name+"--"+number+"--"+type;
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Toast.makeText(getApplicationContext(), data, Toast.LENGTH_LONG).show();
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, null)
.withValue(RawContacts.ACCOUNT_NAME, null)
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, backRefIndex)
.withValue(ContactsContract.Data.MIMETYPE
,ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name)
.build());
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, backRefIndex)
.withValue(Phone.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.NUMBER, number)
.withValue(Phone.TYPE, type).build());
try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) { }
}
它正在运行,没有任何错误。但是,当从手机的联系人列表中查看添加的联系人时,会出现错误消息"Unfortunately contacts have stopped working"
。
代码有什么问题?