如何通过意图添加带有名字和姓氏的联系人
我正在尝试启动 android 本机“添加或编辑联系人”活动,其中一些数据已存在于表单中。这是我当前使用的代码:
Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
intent.putExtra(Insert.NAME, "A name");
intent.putExtra(Insert.PHONE, "123456789");
startActivity(intent);
我的问题是我想指定名字和姓氏。我还注意到有一个 StructuredName 类,其中包含我需要的所有字段的常量标识符。不幸的是,我无法将 StructuredName 字段添加到意图中...
有人知道这是如何正确完成的吗?
注意:我不想直接添加联系人,但我想打开一个填充的“添加联系人”对话框!
谢谢 呃
I am trying to launch the android native "add or edit contact" activity with some data already in the form. This is the code I am using currently:
Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
intent.putExtra(Insert.NAME, "A name");
intent.putExtra(Insert.PHONE, "123456789");
startActivity(intent);
My problem is that I would like to specify a first name and a last name. I also noticed that there is a StructuredName class which contains constant identifiers for all fields I require. Unfortunately, I was unable to add the StructuredName fields to the intent...
Does anybody know how this is done properly?
Note: I am not trying to add the contact directly, but I want to open a populated "add contact" dialog!
Thanks
Duh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自
ContactsContract.Intents.Insert
的大多数/所有值都在默认联系人应用程序的model/EntityModifier.java
类中进行处理 - 这只是填充来自的值将.NAME
插入到StructuredName.GIVEN_NAME
中。您可以尝试将其导入为 vCard 2.1 (text/x-vcard),它支持所有名称组件,但要求您将 vCard 文件转储到 sdcard 上或提供
ContentResolver#openInputStream(Uri) 可以读取(通常是 sdcard 上的文件或指向您自己的 ContentProvider 的 URI)。
一个使用 ContentProvider 动态创建 vCard 的简单示例:
在您的 Activity 中:
在您的 ContentProvider 中(为 ACTION_VIEW Intent 中使用的权限注册):
当触发时,应插入一个联系人,其名称为您在您的路径中放置的任何名称。 Uri 进入电话簿。如果用户有多个联系人帐户,他/她将被要求选择一个。
注意: vCard 的正确编码当然完全被忽略。我想象大多数版本的联系人应用程序应该也支持 vCard 3.0,它不像 vCard 2.1 那样具有脑死亡的编码。
从好的方面来说,此方法还允许您添加工作/手机号码和其他号码(以及更多)。
Most/all values from
ContactsContract.Intents.Insert
are processed in themodel/EntityModifier.java
class in the default contacts application - and that just stuffs the value fromInsert.NAME
intoStructuredName.GIVEN_NAME
.You could try importing it as a vCard 2.1 (text/x-vcard), that supports all the name components but require that you either dump your vCard file on the sdcard or supply something that
ContentResolver#openInputStream(Uri)
can read (typically a file on the sdcard or an URI pointing to your own ContentProvider).A simple example that uses a ContentProvider to create the vCards dynamically:
In your Activity:
In your ContentProvider (registered for the authority used in the ACTION_VIEW Intent):
This should, when triggered, insert a contact named whatever you put in the path of your Uri into the phone book. If the user has several contacts accounts he/she will be asked to select one.
Note: Proper encoding of the vCard is of course completely ignored. I image most versions of the contacts app should support vCard 3.0 also which doesn't have quite as brain-dead encoding as vCard 2.1.
On the up-side, this method will also allow you to add work/mobile and other numbers (and more).