How to add new contact numbers to existing contacts? - android

I have the following code to update a contact number, but the problem is, it only replaces an existing one
I would like to ADD a new number and keep the numbers that already exists in the contact, if it has 2 contact numbers, I would like to be able to add a third one even if it already has a MOBILE_NUMBER in the contact, I would like to be able to add a new one.
I tried a lot of codes here in stackoverflow but I couldn't find any to do it.
from what I've been searching, When you add a new number to an existing contact, you actually create a new contact and associate to the same contact_id. but I couldn't do it.
public static void updateContactNumber (String contactId, String newNumber, Activity act)
{
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
String selectPhone = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='" +
Phone.CONTENT_ITEM_TYPE + "'" + " AND " + Phone.TYPE + "=?";
String[] phoneArgs = new String[]{contactId, String.valueOf(Phone.TYPE_WORK)};
ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(selectPhone, phoneArgs)
.withValue(Phone.NUMBER, "44441111")
.build());
try {
act.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();
}
}

Related

Change contact name programmatically

I'm trying to change specific contact (by contactId) it's display name programmatically.
I've looked into similar q&a here and tried the following:
public static boolean updateContactName(Context context, long contactId, String newName) {
try {
ContentResolver contentResolver = context.getContentResolver();
ArrayList<android.content.ContentProviderOperation> ops = new ArrayList<>();
ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI)
.withSelection(ContactsContract.CommonDataKinds.Phone._ID + "=? AND " +
Data.MIMETYPE + "='" +
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'",
new String[]{Long.toString(contactId)})
.withValue(CommonDataKinds.StructuredName.DISPLAY_NAME, newName)
.build());
contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
return true;
} catch (Exception e) {
CLog.d(ContactUtils.class, e);
return false;
}
}
It's working on some devices but in other (Such as LG G3) it's not working, Nothing is being update.
Thanks for the help!
Please check it out :
try {
String newName = txtName.getText();
ArrayList<ContentProviderOperation> cpoList = new ArrayList<ContentProviderOperation>();
cpoList.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(ContactsContract.CommonDataKinds.Phone._ID + "=? AND " +
Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'", new String[]{contact_id})
.withValue(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, newName)
.build());
ContentProviderResult[] result = getContentResolver().applyBatch(ContactsContract.AUTHORITY, cpoList);
} catch (Exception e) {
// Do Any thing
}

How to delete all android contacts that start with a specific name?

I want to delete all contacts from my android phone that starts with a "AAA" or that contains "AAA" . Here's what I tried:
private void deleteContact(String name) {
ContentResolver cr = getContentResolver();
String where = ContactsContract.Data.DISPLAY_NAME + " = ? ";
String[] params = new String[] {"AAAA"};
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI)
.withSelection(where, params)
.build());
Log.e(",,,,",String.valueOf(ops.get(0)));
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();
}
Toast.makeText(NativeContentProvider.this, "Deleted the contact with name '" + name +"'", Toast.LENGTH_SHORT).show();
}
but failed. Please give me some idea so I can proceed further in my project.
Maybe this will get you on the right track:
ContentResolver mContentResolver = getContentResolver();
private int deleteContactsLike(String name) {
return mContentResolver.delete(
ContactsContract.RawContacts.CONTENT_URI,
ContactsContract.Contacts.DISPLAY_NAME
+ " like ?",
new String[] { name + '%'});

android contacts updation crasing

i get an error "the bind value at index 2 is null" at the line " this.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
This crash occurs only when i try to update many contacts together, and works fine for small numbers of contacts.
I have the contacts displayed in a listview and on selection of the contacts using a checkbox and onClick of a button the below method is called.
Even though the app crashes, the updation does occur.
public void updateContact(String contactId, String type) {
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
String selectPhone = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "='" +
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'" + " AND " + ContactsContract.CommonDataKinds.Phone.TYPE + "=?";
String[] phoneArgs = new String[]{contactId, type};
ops.add(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
.withSelection(selectPhone, phoneArgs).build());
try {
this.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
e.printStackTrace();
} catch (OperationApplicationException e) {
e.printStackTrace();
}

Update contact with multiple phone numbers

I have this code
public void updateContact (String contactId, String newNumber) {ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
String selectPhone = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='" +
Phone.CONTENT_ITEM_TYPE + "'";
String[] phoneArgs = new String[]{contactId};
ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(selectPhone, phoneArgs)
.withValue(Phone.NUMBER, newNumber)
.build());
try { 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();
}
}
which i took from here How to update contact number using Android and changed it in order to pass through all my contacts and add to each one of them a prefix of my own.
I run through all contacts and i provide with this
String id = phones.getString(phones.getColumnIndex(ContactsContract.Contacts._ID));
the Contact's id to the function above. But some contacts Especially the ones that have multiple numbers and some that have only one number, do not change to get the new prefix even though the given id is correct?! .
Do i miss something here i don't know what to change. I think it may be the mime type but i can imagine that someone may not used the Android preinstalled types for phones and used a custom type.
I do not get any errors. Thanks everyone for your time!!!
I finally changed my code to work properly, i was passing the contact ID but i needed the phone id ...here is the correct code.
public void updateContact (String contactId, String newNumber) {
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
String selectPhone = Data._ID + "=? AND " + Data.MIMETYPE + "='" +
Phone.CONTENT_ITEM_TYPE + "'";
String[] phoneArgs = new String[]{contactId};
ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(selectPhone, phoneArgs)
.withValue(Phone.NUMBER, newNumber)
.build());
try {
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();
}
}
Do not forget to set the appropriate permissions on android manifest (write contacts)

I am not able to update my email in android application using content provider

i am working on the application which retrieve all the android contacts and also can delete and update contacts.everything working fine problem is when contact does not have email address then i can not update it using my application (can not add email address from my update contact page). i have written used code below. thanks in advance.
private void updateEmaill(String id, String email) {
ContentResolver cr = getContentResolver();
String where = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND "
+ ContactsContract.Data.MIMETYPE + " = ?";
String[] params = new String[] { id,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE };
Cursor phoneCur = managedQuery(ContactsContract.Data.CONTENT_URI, null,
where, params, null);
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
if ((null == phoneCur)) {
System.out.println("we have got null value from the cursor");
} else {
System.out.println("phone cursor count" + phoneCur.getCount());
ops.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(where, params)
.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS,
email).build());
}
phoneCur.close();
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();
}
}
newUpdate() has been known to work only if a given field exists already.
As a workaround, here's one simple logic you can use:
-check if email exists for contacts
-if it does call update
-if it doesn't save all the fields of this contact in some variables and delete the contact
-use newInsert() to create a new contact with the saved fields from old contact and also the email that you need to add.

Categories

Resources