在Android 2.2中插入联系人
我正在尝试插入新的 RawContact
联系人,但当我通过联系人或电话簿查看联系人时,添加的 RawContact
不会显示。据我了解,如果我们创建一个 RawContact
并且没有与之关联的联系人,那么该联系人将被自动插入。我获得了 rawContactId
的有效值,并且没有抛出任何异常,因此我假设插入成功。我做错了什么或者我错过了什么吗?我正在使用开发人员网站的代码示例,只需将其粘贴到此处:
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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我以为这个问题早就被遗忘了,但既然有人投票了,我假设其他人也面临着和我一样的问题。经过一番努力,我能够找出问题并插入联系人,希望这有帮助,这里是示例代码:
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:
一位客户向我报告,上述答案(由 Als 提供)中的解决方案在某些 HTC 设备上不起作用。经过几天的沮丧,我想出了这个解决方案:
A client reported to me that the solution in the answer above(by Als) doesn't work on some HTC devices. So after a few days of frustration I came up with this solution:
要创建可见联系人,它需要属于可见组。查看计算机上的 Gmail 联系人以查看组和可见性。
要在设备上找到可见的组,请执行以下操作:
要将组添加到 rawContact:
或 ops 版本:
@anqe1ki11er:
我不明白您答案中的第三段,其中写着:
values.put(Data .MIMETYPE、数据.CONTENT_TYPE)
...
没有这样的 MIMETYPE。 (我在运行 HTC Android 2.2 的 HTC Desire 上检查过)。
谢谢。
To have a visible contact created, it needs to belong to a visible group. Have a look at gmail contacts on your computer to view groups and visibility.
To find a visible group on the device, do something like this:
To add the group to the rawContact:
Or the ops version:
@anqe1ki11er:
I don't understand the 3rd paragraph in your answer where it says:
values.put(Data.MIMETYPE, Data.CONTENT_TYPE)
...
There is no such MIMETYPE. (I checked it on HTC Desire running HTC Android 2.2).
Thanks.