如何添加使用的名字,并通过意向姓联系人 [英] How to add a contact with first name and last name via intent

查看:155
本文介绍了如何添加使用的名字,并通过意向姓联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想推出的Andr​​oid原生的形式添加或编辑联系人活动的一些数据了。这是code我使用目前:

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

我的问题是,我想指定一个名字和姓氏。我也注意到,有它包含了我所需要的所有字段的常数标识符一StructuredName类。不幸的是,我无法在StructuredName字段添加到意图...

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!

谢谢 咄

推荐答案

大多数/从 ContactsContract.Intents.Insert 所有值在被处理的模型/ EntityModifier.java 类的默认联系人应用程序 - 而这只是充塞从 Insert.NAME 的价值为 StructuredName .GIVEN_NAME

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.

您可以尝试导入它作为一个电子名片2.1(文/ X-电子名片),支持所有的名字组成部分,但需要你要么转储vCard文件的SD卡或提供的东西, ContentResolver的# openInputStream(URI)可以读取(通常在SD卡或URI指向你自己的ContentProvider文件)。

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

使用ContentProvider的动态创建的电子名片一个简单的例子:

A simple example that uses a ContentProvider to create the vCards dynamically:

在你的活动:

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意图使用权限):

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();
  }
}

这应该触发时,插入一个名为无论你把你的URI的路径进入电话簿联系人。如果用户有多个联系人账户,他/她将被要求选择一个。

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.

注意:在电子名片的的正确的编码,当然是完全忽略。联系人应用程序中我最形象版本的的支持名片3.0还不具备相当的脑死亡的编码作为电子名片2.1。

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

这篇关于如何添加使用的名字,并通过意向姓联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆