This is the problem I am facing. I already have a contact named "Frank" in my contact. I need to add a new contact named "Frank". However, If I add using the below method, The new number gets added to the existing "Frank contact".
How to properly add contacts with the same name?
ArrayList<ContentProviderOperation> ops = new ArrayList<>();
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accountName)
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, HelperMethods.capitalize(name_et.getText().toString()))
.build());
ops.add(ContentProviderOperation.
newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number_et.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM)
.withValue(ContactsContract.CommonDataKinds.Phone.LABEL, phone_label_spinner.getSelectedItem().toString())
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
Found the answer from here: Android not adding all contacts with duplicate fields
Adding AGGREGATION_MODE column will work.
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accountName)
.withValue(ContactsContract.RawContacts.AGGREGATION_MODE, ContactsContract.RawContacts.AGGREGATION_MODE_DISABLED)
.build());
Related
I am trying to create a contact using ContentProviderOperation where basic idea is to create a contact which might don't have Phone or Mobile number at all but may have some other type of contact number which is adding using Phone.TYPE_CUSTOM with different label. Here is a code:
//ArrayList<ContentProviderOperation> ops;
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, mobileNumber)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phoneNumber)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_HOME)
.build());
and some contact type:
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phoneNumber)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM )
.withValue(ContactsContract.CommonDataKinds.Phone.LABEL, "Emergency: "+name+" "+surname+" 11111")
.build());
it don't create a main contact if both mobileNumber and phoneNumber are empty so if i provide some value of phoneNumber/mobileNumber then contact is create which containing custom contact information as well. So is there any way to create contact without Phone.TYPE_HOME/Phone.TYPE_MOBILE ?
i want to enter contact list: "contact_name" , and "contact_number" into phone's contact list i used following code for that :
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());
//------------------------------------------------------ Names
ops.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, name).build());
//------------------------------------------------------ Mobile Number
ops.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, phone)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
.build());
// Asking the Contact provider to create a new
// contact
try
{
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}
catch (Exception e)
{
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Exception: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
it run successfully, but i want to prevent insertion of duplicate entry,
for that i have used below link but could not find out proper solution
contact exists in contacts
I am developing a backup application for Android, mainly contacts and SMS messages. Backing up isn't a problem, but writing the contacts back to the Android database is proving problematic.
This is what I have so far:
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, 1)
.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.GIVEN_NAME, "Joe")
.withValue(StructuredName.FAMILY_NAME, "Bloggs")
.build());
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID,1)
.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, "086555555")
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
.build());
try{
ctx.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}catch(Exception e){
e.printStackTrace();
}
It all seems to hinge on the RAW_CONTACT_ID. I'm using the emulator, with no contacts at the start. It works for the first contact creation(RAW_CONTACT_ID = 0), but no contacts seem to be created after that initial one, where RAW_CONTACT_ID is 1 upwards. Anybody got any ideas as to how this is the case?
What I did was I created my contact first and then grabbed the ID of that newly created contact and added all the other contact details to that ID
Here is my frist step, create the new contact, then find the ID of that contact
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());
ops.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.PREFIX, prefix)
.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, givenName)
.withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, middleName)
.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, familyName)
.withValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX, suffix)
.build());
resolver.applyBatch(ContactsContract.AUTHORITY, ops);
Cursor cursor = null;
try
{
cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, baseProjection, ContactsContract.Contacts.DISPLAY_NAME + " = ? ", new String[] {displayname}, __DEFAULT_SORT_ORDER);
if (cursor.moveToFirst())
{
String val;
val = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
return val;
}
}
...
Then I add any detail to the contact I want like this:
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.CONTACT_ID, contactId)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.Data.DATA5, poBox)
.withValue(ContactsContract.Data.DATA4, street)
.withValue(ContactsContract.Data.DATA7, city)
.withValue(ContactsContract.Data.DATA8, region)
.withValue(ContactsContract.Data.DATA9, postCode)
.withValue(ContactsContract.Data.DATA10, country)
.withValue(ContactsContract.Data.DATA2, type)
.withValue(ContactsContract.Data.DATA3, label)
.build());
I need to add a contact in a contacts group. The group is added but the contact is not added in the group.
Here is my code. I think the exception occurs in
ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID
but i don't know why.. Can you can help me please?
ops.add(ContentProviderOperation
.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE,accountType)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME,accountName)
.build());
ops.add(ContentProviderOperation
.newAssertQuery(ContactsContract.Groups.CONTENT_URI)
.withValueBackReference(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, 0)
.withSelection(ContactsContract.Groups.TITLE + "=?", new String[]{grupo})
.withExpectedCount(1)
.build());
ops.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, nome)
.build());
ops.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.DATA1, mail)
.build());
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, 0)
.withValue(ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE,
ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE)
.build());
try {
resolver.applyBatch(ContactsContract.AUTHORITY, ops);
} catch(Exception e) {
e.printStackTrace();
}
The person who wrote the method you're calling incorrectly decided to throw that exception if you passed in any data that it couldn't handle. I'd recommend consulting the javadocs to see if there's an explanation there.
It's called checking pre-conditions; programming by contract.
This code is only insert new contact into All other contacts in "Display Options".
I don't know how to add to System group: my contacts.
here my code:
//Add contact type
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.google")
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, "aa#gmail.com")
.build());
//Add contact name
ops.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, (!name.toLowerCase().equals("unavailable") && !name.equals("")) ? name : phoneNumber)
.build());
//Add phone number
ops.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, phoneNumber)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, phoneType)
.build());
//Add contact
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
have any properties support for this solution?