我正在尝试以编程方式编辑联系人详细信息。我想更改电话号码等于123的联系人姓名。这是我的非工作代码。
Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode("123"));
// This query will return NAME and ID of contact, associated with phone //number.
Cursor mcursor = getContentResolver().query(lookupUri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID}, null, null, null);
//Now retrive _ID from query result
long idPhone = 0;
try {
if (mcursor != null) {
if (mcursor.moveToFirst()) {
idPhone = Long.valueOf(mcursor.getString(mcursor.getColumnIndex(ContactsContract.PhoneLookup._ID)));
String getID = String.valueOf(idPhone);
Toast.makeText(this.getApplicationContext(), getID, Toast.LENGTH_LONG).show();
Uri uri= ContentUris.withAppendedId(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,3625);
ContentValues values = new ContentValues();
values.put(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,"After Changed name");
getContentResolver().update(uri, values, ContactsContract.CommonDataKinds.Phone._ID+"=?", new String[] {String.valueOf(idPhone)});
}
}
} finally {
mcursor.close();
}
目前,我正在查找电话号码并获取电话号码的ID。使用ID我正在尝试更新它,似乎它没有使用ID更新电话号码。
答案 0 :(得分:0)
这是我的工作代码。
public void updateContact (String contactId, String newNumber, Activity act) throws RemoteException, OperationApplicationException{
//ASSERT: @contactId alreay has a work phone number
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, newNumber)
.build());
act.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}
//hope this also work for you.