如何以编程方式在我的联系人列表中的现有联系人中添加电话号码?我知道如何添加或删除联系人,但我无法在其中一个联系人中添加电话号码......所以,请帮助我。
答案 0 :(得分:2)
ArrayList<ContentProviderOperation> ops = new ArrayList<>();
//...
//add Phone to existiong Contact
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
.withValue(Phone.NUMBER, delta.getAsString(Phone.NUMBER))
.withValue(Phone.TYPE, delta.getAsString(Phone.TYPE)).build());
//...
//add Email to existiong Contact
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
.withValue(Email.ADDRESS, delta.getAsString(Email.ADDRESS))
.withValue(Email.TYPE, delta.getAsString(Email.TYPE)).build());
//...
try {
mContext.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
Log.d(TAG, "update success");
} catch (Exception e) {
Log.d(TAG, "update failed");
e.printStackTrace();
}
rawContactId
是android.provider.ContactsContract.RawContacts._ID
您可以使用Contacts.CONTENT_URI
进行查询以获取此
delta
为RawContactDelta
,您可以使用任何字符串值替换delta.getAsString(Phone.NUMBER)
,delta.getAsString(Email.ADDRESS)
答案 1 :(得分:0)
这是示例。
try {
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.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, "anything")
.build());
ContentProviderResult[] result = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
}