更新已存在的联系人与多号机器人

时间:2013-11-26 09:51:53

标签: android contacts android-contentprovider android-contacts

我正在使用此代码更新android上现有联系人的号码:

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ContentProviderOperation.Builder builder = ContentProviderOperation
                .newUpdate(ContactsContract.Data.CONTENT_URI)
                .withSelection(
                        ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "=?"
                                + " AND " + ContactsContract.CommonDataKinds.Phone.TYPE + "=?",
                        new String[] { String.valueOf(contactId),
                                ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE })
                .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, mobileNumber);
ops.add(builder.build());

我想为此联系人添加另一个号码,其中Type = ContactsContract.CommonDataKinds.Phone.TYPE_WORK不清楚旧号码。

如何使用ContentProviderOperation

执行此操作

1 个答案:

答案 0 :(得分:2)

好的,我找到了解决方案

builder = ContentProviderOperation
                    .newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValue(ContactsContract.Data.RAW_CONTACT_ID, rawcontactId)
                    .withValue(ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                    .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number)
                    .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, String.valueOf(type));
            ops.add(builder.build());