Android not adding all contacts with duplicate fields

南楼画角 提交于 2019-12-23 05:48:05

问题


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!


回答1:


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



回答2:


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.




回答3:


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)



来源:https://stackoverflow.com/questions/4175737/android-not-adding-all-contacts-with-duplicate-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!