二百多条记录差不多要七分钟
使用下面批量添加方法,不到两分钟
/**
* 批量添加通讯录
*
* @throws OperationApplicationException
* @throws RemoteException
*/
public static void BatchAddContact(List list)
throws RemoteException, OperationApplicationException {
GlobalConstants.PrintLog_D("[GlobalVariables->]BatchAddContact begin");
ArrayList ops = new ArrayList();
int rawContactInsertIndex = 0;
for (Tb_contacts contact : list) {
rawContactInsertIndex = ops.size(); // 有了它才能给真正的实现批量添加
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, null)
.withValue(RawContacts.ACCOUNT_NAME, null)
.withYieldAllowed(true).build());
// 添加姓名
ops.add(ContentProviderOperation
.newInsert(
Android.provider.ContactsContract.Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID,
rawContactInsertIndex)
.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.DISPLAY_NAME, contact.getName())
.withYieldAllowed(true).build());
// 添加号码
ops.add(ContentProviderOperation
.newInsert(
android.provider.ContactsContract.Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID,
rawContactInsertIndex)
.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.NUMBER, contact.getNumber())
.withValue(Phone.TYPE, Phone.TYPE_MOBILE)
.withValue(Phone.LABEL, "").withYieldAllowed(true).build());
}
if (ops != null) {
// 真正添加
ContentProviderResult[] results = mContext.getContentResolver()
.applyBatch(ContactsContract.AUTHORITY, ops);
// for (ContentProviderResult result : results) {
// GlobalConstants
// .PrintLog_D("[GlobalVariables->]BatchAddContact "
// + result.uri.toString());
// }
}
}
通过以上的对比,对于数据库效率就不一样了!