我正在尝试更新电话簿中的联系人,但我需要检查联系人是否在哪个头部TYPE_WORK,TYPE_MOBILE等....
public void updateContact (String newNumber,String oldNumber)
throws RemoteException, OperationApplicationException {
//ASSERT: @contactId alreay has a work phone number
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
String selectPhone = ContactsContract.CommonDataKinds.Phone.NUMBER + "=? AND " + ContactsContract.Data.MIMETYPE + "='" +
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'" + " AND " + ContactsContract.CommonDataKinds.Phone.TYPE + "=?";
String[] phoneArgs = new String[]{oldNumber, String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)};
if(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectPhone, phoneArgs).equals(""))
{
Toast.makeText(getContext(), "MOBILE FIELD IS EMPTY", Toast.LENGTH_SHORT).show();
phoneArgs = new String[]{oldNumber, String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_WORK)};
if(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectPhone, phoneArgs).equals(""))
{
Toast.makeText(getContext(), "Work FIELD IS EMPTY", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getContext(), "Work FIELD IS NOT EMPTY", Toast.LENGTH_SHORT).show();
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectPhone, phoneArgs)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, newNumber)
.build());
}
}
else {
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectPhone, phoneArgs)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, newNumber)
.build());
}
getContext().getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}
我尝试过但不成功..PLs帮助
答案 0 :(得分:1)
您需要检索联系人ID和联系人类型,并在更新查询参数中使用它。我试过这个并且它有效。 -
public void updateContact(String newNumber, String oldNumber) throws RemoteException, OperationApplicationException {
// get contact type and contact id
String contactId = null;
int contactType = -1;
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(oldNumber));
Cursor cursor = getContentResolver().query(
uri,
new String[]{ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.TYPE},
null,
null,
null);
if (cursor != null) {
while (cursor.moveToNext()) {
contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
contactType = cursor.getInt(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.TYPE));
}
cursor.close();
}
//ASSERT: @contactId alreay has a work phone number
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 = null;
if (contactType == ContactsContract.CommonDataKinds.Phone.TYPE_WORK) {
Toast.makeText(this, "MOBILE FIELD IS EMPTY", Toast.LENGTH_SHORT).show();
phoneArgs = new String[]{contactId, String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_WORK)};
} else if (contactType == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) {
Toast.makeText(this, "Work FIELD IS EMPTY", Toast.LENGTH_SHORT).show();
phoneArgs = new String[]{contactId, String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)};
}
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectPhone, phoneArgs)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, newNumber)
.build());
ContentProviderResult[] contentProviderResults = this.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
if (contentProviderResults[0].count > 0) {
Toast.makeText(this, "Updated Contact Successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Contact could not be updated", Toast.LENGTH_SHORT).show();
}
}
确保您拥有读取和写入联系人的权限。 希望这有帮助!