如何通过意图添加带有名字和姓氏的联系人

发布于 2024-12-06 19:07:52 字数 502 浏览 0 评论 0原文

我正在尝试启动 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜中书 2024-12-13 19:07:52

来自 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 中:

Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("content://some.authority/N:Jones;Bob\nTEL:123456790\n"), "text/x-vcard");
startActivity(i);

在您的 ContentProvider 中(为 ACTION_VIEW Intent 中使用的权限注册):

public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
  try {
    FileOutputStream fos = getContext().openFileOutput("filename.txt", Context.MODE_PRIVATE);
    String vcard = "BEGIN:VCARD\nVERSION:2.1\n" +
        uri.getPath().substring(1) +
        "END:VCARD\n";
    fos.write(vcard.getBytes("UTF-8"));
    fos.close();
    return ParcelFileDescriptor.open(new File(getContext().getFilesDir(), "filename.txt"), ParcelFileDescriptor.MODE_READ_ONLY);
  } catch (IOException e) {
    throw new FileNotFoundException();
  }
}

当触发时,应插入一个联系人,其名称为您在您的路径中放置的任何名称。 Uri 进入电话簿。如果用户有多个联系人帐户,他/她将被要求选择一个。

注意: vCard 的正确编码当然完全被忽略。我想象大多数版本的联系人应用程序应该也支持 vCard 3.0,它不像 vCard 2.1 那样具有脑死亡的编码。

从好的方面来说,此方法还允许您添加工作/手机号码和其他号码(以及更多)。

Most/all values from ContactsContract.Intents.Insert are processed in the model/EntityModifier.java class in the default contacts application - and that just stuffs the value from Insert.NAME into StructuredName.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:

Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("content://some.authority/N:Jones;Bob\nTEL:123456790\n"), "text/x-vcard");
startActivity(i);

In your ContentProvider (registered for the authority used in the ACTION_VIEW Intent):

public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
  try {
    FileOutputStream fos = getContext().openFileOutput("filename.txt", Context.MODE_PRIVATE);
    String vcard = "BEGIN:VCARD\nVERSION:2.1\n" +
        uri.getPath().substring(1) +
        "END:VCARD\n";
    fos.write(vcard.getBytes("UTF-8"));
    fos.close();
    return ParcelFileDescriptor.open(new File(getContext().getFilesDir(), "filename.txt"), ParcelFileDescriptor.MODE_READ_ONLY);
  } catch (IOException e) {
    throw new FileNotFoundException();
  }
}

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文