Android 不添加所有具有重复字段的联系人

发布于 2024-10-02 12:11:13 字数 1143 浏览 2 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空袭的梦i 2024-10-09 12:11:13

经过 2 个不睡觉的日子...重读我的代码并重写我的类
我找到了解决方案:有一个我不知道的列,它甚至不存在于早期的 API 中,称为:AGGREGATION_MODE,

所以解决方案是这样的:

    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)
             .withValue(RawContacts.AGGREGATION_MODE, RawContacts.AGGREGATION_MODE_DISABLED)

             .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();
      }

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:

    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)
             .withValue(RawContacts.AGGREGATION_MODE, RawContacts.AGGREGATION_MODE_DISABLED)

             .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();
      }
九命猫 2024-10-09 12:11:13

联系人被合并到新的 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.

水晶透心 2024-10-09 12:11:13

您应该尝试使用 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文