我使用以下代码更新联系人:
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] {Phone.NUMBER, ContactsContract.Data.CONTACT_ID}, null, null, null);
while (cursor.moveToNext()) {
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.CONTACT_ID));
ArrayList<ContentProviderOperation> ops =
new ArrayList<ContentProviderOperation>();
Builder builder= ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
builder.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " +
ContactsContract.Data.MIMETYPE + "=?"+ " AND " +
ContactsContract.CommonDataKinds.Organization.TYPE + "=?", new String[]{contactId,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_HOME)});
builder.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, newNumber);
ops.add(builder.build());
try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
Log.i("SirwanError", e.getMessage());
} catch (OperationApplicationException e) {
Log.i("SirwanError", e.getMessage());
}
}
程序无误地运行,但联系人不会更新 有什么想法吗?
答案 0 :(得分:0)
最后我用这种方式解决了我的问题:
ContentValues contentValues = new ContentValues();
contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
contentValues.put(ContactsContract.CommonDataKinds.Phone.NUMBER, newNumber);
contentValues.put(ContactsContract.CommonDataKinds.Phone.TYPE, contactType);
contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, contactId);
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newUpdate(
ContactsContract.Data.CONTENT_URI)
.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " +
ContactsContract.Data.MIMETYPE + "=?"+ " AND " +
ContactsContract.CommonDataKinds.Organization.TYPE + "=?", new String[]{contactId,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
String.valueOf(contactType)}).withValues(contentValues).build());
try
{
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}
catch (Exception e)
{
Log.e("Error", e.getMessage());
}