Add contact to a group in Android app - android

I am working on android apps. I want to add a contact in Android phone group. The code I am using is shown below but the group doesn't add to grouplist in phone:
private void addContact(String name, String phone, String group) {
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, null)
.withValue(RawContacts.ACCOUNT_NAME, null)
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.DISPLAY_NAME, name)
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.NUMBER, phone)
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Groups.CONTENT_URI)
.withValue(ContactsContract.Groups.GROUP_VISIBLE, 1)
.withValue(ContactsContract.Groups.TITLE, group)
.build());
try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}
catch (RemoteException e) {
e.printStackTrace();
}
catch (OperationApplicationException e) {
e.printStackTrace();
}
}
}

Related

How to update 'profile' contact programmatically in Android?

I want to edit the device main user's profile from within my application. So I used the ContentProviderOperation with newUpdate method to collect the changes and then commit them using the ContentResolver method's : applyBatch. Here is the code i wrote so far:
public ContentProviderResult[] updateProfile(Contact userProfile) {
try {
ArrayList<ContentProviderOperation> ops = new ArrayList<>();
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, Integer.valueOf(userProfile.id))
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, edtName.getText().toString().trim())
.build());
return getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
I am getting any run-time Exception but no change is commited!
Am I missing something?
I have figured it out, the problem was that I didn't know how to retrieve the User Profile RAW_CONTACT_ID to project on it on the ContactsContract.Data table.
My quick fix was by deleting the User Profile record and then create a new one. This is the code I used so far :
public ContentProviderResult[] createOrUpdateUserProfile() {
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
try {
ops.add(ContentProviderOperation.
newDelete(ContactsContract.Profile.CONTENT_RAW_CONTACTS_URI)
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
ops.clear();
//Create User Profile
ops.add(ContentProviderOperation.newInsert(ContactsContract.Profile.CONTENT_RAW_CONTACTS_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());
//Create User Name
if (!(edtName.getText().toString().matches(""))) {
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, edtName.getText().toString())
.build());
}
if (!(edtHomePhone.getText().toString().matches(""))) {
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, edtHomePhone.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 1)
.build());
}
if (!(edtMobilePhone.getText().toString().matches(""))) {
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, edtMobilePhone.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 2)
.build());
}
if (!(edtWorkPhone.getText().toString().matches(""))) {
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, edtWorkPhone.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 3)
.build());
}
if (!(edtHomeEmail.getText().toString().matches(""))) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, edtHomeEmail.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 1) // contact.numbers.get(0).type
.build());
}
if (!(edtWorkEmail.getText().toString().matches(""))) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, edtWorkEmail.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 2) // contact.numbers.get(0).type
.build());
}
/*String completeAddress = edtStreet.getText().toString() + edtCity.getText().toString() + edtState.getText().toString()
+ edtCountry.getText().toString();*/
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, edtStreet.getText().toString())
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, edtCity.getText().toString())
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, edtState.getText().toString())
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, edtCountry.getText().toString())
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, edtZip.getText().toString())
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, 1)
.build());
if (!(edtSkype.getText().toString().matches(""))) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Im.DATA, edtSkype.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Im.PROTOCOL, 3)
.build());
}
ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
return res;
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
your code has a a bit error.
public ContentProviderResult[] updateProfile(Contact userProfile) {
try {
ArrayList<ContentProviderOperation> ops = new ArrayList<>();
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, Integer.valueOf(userProfile.id)) // Error is here
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, edtName.getText().toString().trim())
.build());
return getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use for this line:
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, Integer.valueOf(userProfile.id))
this
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, getRawContactId(id))
public static String getRawContactId(String contactId)
{
String res = "";
Uri uri = ContactsContract.RawContacts.CONTENT_URI;
String[] projection = new String[]{ContactsContract.RawContacts._ID};
String selection = ContactsContract.RawContacts.CONTACT_ID + " = ?";
String[] selectionArgs = new String[]{ contactId };
Cursor c = ContentManager.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if(c != null && c.moveToFirst())
{
res = c.getString(c.getColumnIndex(ContactsContract.RawContacts._ID));
c.close();
}
return res;
}

How to add suffix name in contacts in android programmatically

I want to add new contact with some details in Contacts list. I want to add Name, Contact number and Suffix name here. I can add Name and Contact number. But I could not add suffix name. I don't know this is right code to add suffix name. I have given my code below.
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactId = ops.size();
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, rawContactId)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
groupInfo.getName()).build());
ops.add(ContentProviderOperation.newInsert(
ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.StructuredName.SUFFIX,
groupInfo.getCompanyName()).build());
ops.add(ContentProviderOperation.
newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, groupInfo.getContactNumber())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
.build());
try {
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
Toast.makeText(context, "This person is added successfully to your contact list", Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
Toast.makeText(context, "Can not add to your contact list", Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (OperationApplicationException e) {
e.printStackTrace();
Toast.makeText(context, "Can not add to your contact list", Toast.LENGTH_LONG).show();
}
Finally, I found solution myselft..
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactId = ops.size();
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, rawContactId)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.StructuredName.SUFFIX,
groupInfo.getCompanyName())
.withValue(
ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
groupInfo.getName())
.build());
ops.add(ContentProviderOperation.
newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, groupInfo.getContactNumber())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
.build());
try {
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
Toast.makeText(context, "This person is added successfully to your contact list", Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
Toast.makeText(context, "Can not add to your contact list", Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (OperationApplicationException e) {
e.printStackTrace();
Toast.makeText(context, "Can not add to your contact list", Toast.LENGTH_LONG).show();
}

Insert contact programmatically in android 4.0 is not showing in default contact list

I'm doing an Android application which insert contacts programmatically.I've added one contact using below code. Its adding up well but I couldn't see that contact on default device contact list. My device version is 4.0.4. But its showing well & good on other devices.Thanks in advance.
Code:
private void createContact(String name, String phone) {
ContentResolver cr = getContentResolver();
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation
.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE,
"accountname#gmail.com")
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME,
"com.google").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,
name).build());// Display name comes here
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)// Phone number comes up here
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_HOME)
.build());
try {
cr.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Unable to add new contact - Android

I am trying to add new contacts from within in my android application. However - when I do it and check my contact list the new contact isn't there? I have ensured the display name is unique and looked at a few threads like How to add new contacts in android but have still had no luck. Any suggestions? I call the contact class from another activity.
public class Contact {
public Contact(String displayName, String emailAddress, Context context) {
super();
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, null)
.withValue(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.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, emailAddress)
.withValue(ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.build());
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID,
rawContactInsertIndex)
.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.DISPLAY_NAME, displayName)
.build());
try {
ContentProviderResult[] res = context.getApplicationContext()
.getContentResolver()
.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Contact intent insert City,Country,ZipCode

I am Inserting contact using following code.
intent = new Intent(Intent.ACTION_INSERT);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
intent.putExtra(ContactsContract.Intents.Insert.COMPANY,ConstantData.mainTable.getCompany_name());
intent.putExtra(ContactsContract.Intents.Insert.POSTAL,ConstantData.mainTable.getAddress1()+" "+ConstantData.mainTable.getAddress2()+" "+ConstantData.mainTable.getAddress3()+",City: America");
intent.putExtra(ContactsContract.Intents.Insert.PHONE,ConstantData.mainTable.getTelephone_number());
intent.putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE,ConstantData.mainTable.getMobile_number());
intent.putExtra(ContactsContract.Intents.Insert.EMAIL,ConstantData.mainTable.getEmail_address());
startActivity(intent);
But I am not able to insert City,Zip-code and Country,So How to insert it using Intent??
I added a contact but not using Intent. Here is the code:
void addingContact(String firstName, String phoneMobile, String street, String poBox, String city, String state, String zipCode, String country)
{
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.GIVEN_NAME, firstName)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName).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, phoneMobile)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
.build());
//--------------------------------- address
ops.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, street)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, city)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, state)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, country)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE,
ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME)
.build());
try
{
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}
catch (Exception e)
{
e.printStackTrace();
// Toast.makeText(this, "Exception: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}

Categories

Resources