I am trying to insert a list of contacts in android using Contact API. But I am facing a strange problem with that. Some of the contact is getting duplicated if they exist in the phone book. But this happens to only a few of the contacts not to all the contacts which confused me more.
Here is my code.
final ArrayList<ContentProviderOperation> restoredContactList = new ArrayList<ContentProviderOperation>();
restoredContactList.clear();
restoredContactList
.add(ContentProviderOperation
.newInsert(
ContactsContract.RawContacts.CONTENT_URI)
.withValue(
ContactsContract.RawContacts.ACCOUNT_TYPE,
mAccountType)
.withValue(
ContactsContract.RawContacts.ACCOUNT_NAME,
mAccountName).build());
for (int j = 0; j < namesArray.length(); j++) {
final JSONObject nameObj = namesArray
.getJSONObject(j);
restoredContactList
.add(ContentProviderOperation
.newInsert(
ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID,
0)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
nameObj.getString("first_name")
+ " "
+ nameObj
.getString("last_name"))
.withValue(
ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
nameObj.getString("first_name"))
.withValue(
ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
nameObj.getString("last_name"))
.build());
}
// Phone
final JSONArray phonesArray = obj
.getJSONArray("phones");
for (int j = 0; j < phonesArray.length(); j++) {
final JSONObject phoneObj = phonesArray
.getJSONObject(j);
restoredContactList
.add(ContentProviderOperation
.newInsert(
ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID,
0)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.Phone.NUMBER,
phoneObj.getString("phone_number"))
.withValue(
ContactsContract.CommonDataKinds.Phone.TYPE,
getTypeOfNumber(phoneObj
.getString("phone_type")))
.build());
}
// Email
final JSONArray emailsArray = obj
.getJSONArray("emails");
for (int j = 0; j < emailsArray.length(); j++) {
final JSONObject mailObj = emailsArray
.getJSONObject(j);
restoredContactList
.add(ContentProviderOperation
.newInsert(
ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID,
0)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.Email.DATA,
mailObj.getString("email_address"))
.withValue(
ContactsContract.CommonDataKinds.Email.TYPE,
getTypeOfEmail(mailObj
.getString("email_type")))
.build());
}
// Address
final JSONArray addressesArray = obj
.getJSONArray("address");
for (int j = 0; j < addressesArray.length(); j++) {
final JSONObject addressObj = addressesArray
.getJSONObject(j);
restoredContactList
.add(ContentProviderOperation
.newInsert(
ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID,
0)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.StructuredPostal.STREET,
addressObj
.getString("street"))
.withValue(
ContactsContract.CommonDataKinds.StructuredPostal.CITY,
addressObj
.getString("city"))
.withValue(
ContactsContract.CommonDataKinds.StructuredPostal.REGION,
addressObj
.getString("state"))
.withValue(
ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE,
addressObj
.getString("zipcode"))
.withValue(
ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY,
addressObj
.getString("country"))
.withValue(
ContactsContract.CommonDataKinds.StructuredPostal.TYPE,
getTypeOfAddress(addressObj
.getString("addressType")))
.build());
}
// Asking the Contact provider to create a new
// contact
getContentResolver().applyBatch(
ContactsContract.AUTHORITY,
restoredContactList);
I am having 128 contacts in my device already.
Basically app is to restore contacts from the server. First time when i restored all contact at that time i deleted all the existing contact from my phone book, it works fine. Now in my phone I am having 128 contacts and I am trying to restore contacts again,but now I got 133 contacts, so 5 of contacts are duplicated in this case. I don't know why is it happening.
I am not getting any error or exception. I think i am making some small mistake,but not able to recognize it. If anyone has any idea,please kindly help me.