在 Android 2.2 中插入联系人 [英] Inserting contacts in Android 2.2
问题描述
我正在尝试插入新的 RawContact
联系人,但是当我通过联系人或电话簿查看联系人时,添加的 RawContact
没有显示出来.据我了解,如果我们创建一个 RawContact
并且没有与之关联的联系人,那么该联系人将被自动插入.我得到一个有效的 rawContactId
值并且没有抛出异常,所以我假设插入是成功的.我做错了什么还是我错过了什么?我正在使用来自开发人员站点的代码示例,只需将其粘贴到此处:
I am trying to insert new RawContact
contacts, but the RawContact
added doesn't get displayed when I view the contacts through Contacts or phonebook. As I understand if we create a RawContact
and there is no contact associated with it then the contact will be automatically inserted. I get a valid value of rawContactId
and no exceptions are thrown, so I assume the insertion is successful. Am I doing anything wrong or am I missing something? I am using the code example from developer site, just pasting it here:
ContentValues values = new ContentValues();
values.put(RawContacts.ACCOUNT_TYPE, accountType);
values.put(RawContacts.ACCOUNT_NAME, accountName);
Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
values.put(StructuredName.DISPLAY_NAME, "Mike Sullivan");
getContentResolver().insert(Data.CONTENT_URI, values);
推荐答案
我以为这个 Q 早就被人遗忘了,但是既然有人点赞了,我假设其他人也面临和我一样的问题.经过一番挣扎,我能够找出问题并插入联系人,希望这会有所帮助,这是示例代码:
I thought this Q was long forgotten but Since someone upvoted it, I am assuming someone else also faces the same problem as me. After a little struggle I was able to figure out the problem and insert contacts, Hope this helps, here is the sample code:
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, null)
.withValue(RawContacts.ACCOUNT_NAME,null )
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE,Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.NUMBER, "9X-XXXXXXXXX")
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE,StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.DISPLAY_NAME, "Mike Sullivan")
.build());
ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
这篇关于在 Android 2.2 中插入联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!