Update multiple contacts with different phone type Android
I have some issues when updating phone number for contacts of different
phone type. I need to add prefix xxx with all the numbers. For example:
"Paul" have Mobile: 42342397908, Home: 3453459534, Work: 4345436533,
Other: 3253454354, Other: 3465465464, Mobile: 34564654654. The update is
working but when it saves it is like this. Mobile: xxx42342397908, Home:
xxx3453459534, Work: xxx4345436533, Other: xx3253454354, Other:
xx3253454354, Mobile: xxx42342397908. It overrides the one which have same
phone type with the same phone number. please see below my update function
and help me.
private void updateContact(String name, String phone,String id, String
type) {
ContentResolver cr = getContentResolver();
String where = ContactsContract.Data.DISPLAY_NAME + " = ? AND " +
ContactsContract.Data.MIMETYPE + " = ? AND " +
String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE)
+ " = ? ";
String[] params = null;
/*
ContentProviderOperation.Builder builderPhone =
ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(ContactsContract.Data.CONTACT_ID +
"=?"+" AND "+ContactsContract.Data.MIMETYPE + "=?" + "
AND "+Phone.TYPE+"=?",
new String[]{String.valueOf(id),
Phone.CONTENT_ITEM_TYPE,
String.valueOf(phoneType)});
*/
if (Integer.valueOf(type) == Phone.TYPE_MOBILE)
{
params = new String[] {name,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)};
}
else if (Integer.valueOf(type) == Phone.TYPE_HOME)
{
params = new String[] {name,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_HOME)};
}
else if (Integer.valueOf(type) == Phone.TYPE_WORK)
{
params = new String[] {name,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_WORK)};
}
else if (Integer.valueOf(type) == Phone.TYPE_OTHER)
{
params = new String[] {name,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_OTHER)};
}
// ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE
Cursor phoneCur =
managedQuery(ContactsContract.Data.CONTENT_URI, null, where,
params, null);
/* */
ArrayList<ContentProviderOperation> ops = new
ArrayList<ContentProviderOperation>();
if ( (null == phoneCur) ) {
createContact(name, phone);
} else {
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(where, params)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,
phone)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
type)
.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();
}
}
No comments:
Post a Comment