Android 不添加所有具有重复字段的联系人
在 Android 2.1 update 1 中添加重复的联系人字段时,我遇到了很大的麻烦,
请看一下我的代码:
ArrayList<ContentProviderOperation> op_list = new ArrayList<ContentProviderOperation>();
op_list.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());
// first and last names
op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, 0)
.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.GIVEN_NAME, "MyFirstName")
.withValue(StructuredName.FAMILY_NAME, "MyLastName")
.build());
try{
ContentProviderResult[] results = cResolver.applyBatch(ContactsContract.AUTHORITY, op_list);
}catch(Exception e){
e.printStackTrace();
}
尝试在 20 次迭代循环中运行这段代码,然后转到联系人应用程序 你会看到那里只有 8 个联系人!! 当我插入重复的电子邮件、电话、组织时也会发生此问题。 从0->200循环尝试一下,android会发疯的!
我的代码有问题吗?有什么办法解决这个问题吗?
任何帮助将非常感激... 谢谢!
I'm having a BIG touble when adding duplicate contact fields in Android 2.1 update 1
please have a look at my code:
ArrayList<ContentProviderOperation> op_list = new ArrayList<ContentProviderOperation>();
op_list.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());
// first and last names
op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, 0)
.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.GIVEN_NAME, "MyFirstName")
.withValue(StructuredName.FAMILY_NAME, "MyLastName")
.build());
try{
ContentProviderResult[] results = cResolver.applyBatch(ContactsContract.AUTHORITY, op_list);
}catch(Exception e){
e.printStackTrace();
}
Try to run this piece of code in a 20 iteration loop, then go to the contacts application
you will see only 8 contacts hanging there!!
This problem happens when also when I insert duplicate emails, phones, organizations.
Try it in a loop from 0->200, android will go crazy!
is there a problem in my code? is there any solution to this?
any help will be really appreciated ...
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
经过 2 个不睡觉的日子...重读我的代码并重写我的类
我找到了解决方案:有一个我不知道的列,它甚至不存在于早期的 API 中,称为:AGGREGATION_MODE,
所以解决方案是这样的:
After 2 non sleep days... rereading my code and rewriting my classes
I found the solution to this: there is a column that I was not aware of, and it didn't even exist in earlier APIs called: AGGREGATION_MODE
so the solution turned out to be this:
联系人被合并到新的 api (sdk >2.0) 中。
属于一组特定规则的所有联系人都会自动合并,以在联系人列表中显示单个联系人。
该套规则包括
1. 名字和姓氏相同的联系人。
2. 具有相同邮件ID 的联系人。
3. 联系人的姓名和电话号码与...等相匹配。
在“显示联系人详细信息”屏幕中,重复项不可见。
如果您尝试编辑联系人,您将找到您输入的联系人的所有实例。 (即,如果某个联系人输入 10 次,则依次可见 10 个不同的实例。)您可以在那里编辑任何特定联系人。
由于 Android 2.0 支持多个帐户,因此这种情况将会存在。
The contacts are merged in new api (sdk >2.0).
All the contacts which fall into a specific set of rules are automatically merged to display a single contact in the contact list.
The set of rules include
1. Contacts having the same First name and Last Name.
2. Contacts having the same mail ID.
3. Contact having a name and a Phone number matching ...etc.
In the Display Contact details screen the duplicates are not visible.
If you try to edit the contact you will find all the instances of the contact that you had entered. (i.e if a contact is entered 10 times then 10 different instances are visible one after the other.) You can edit any particular contact there.
As Android 2.0 supports multiple Accounts this scenario would exists.
您应该尝试使用 ContentResolver 直接插入,也许会有帮助(我没有尝试插入 20 次相同的联系人,所以我不知道这是否会产生任何影响)
You should try with direct insertion with a ContentResolver , maybe it would help (I haven't tried to insert 20 times the same contact, so I don't know if it will make any difference)