如何结构化的数据添加到一个新的联系人意向 [英] How to add structured data to a new contact Intent

查看:196
本文介绍了如何结构化的数据添加到一个新的联系人意向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要支持Android 2.1及更高版本。

I need to support Android 2.1 and up.

谷歌公布了如何使用ContactsContract的例子,但它的一些使用的东西,都是开始提供API级别11,所以我需要即兴,但我坚持。

Google posted an example of how to use the ContactsContract, but some of it used things that are available starting with API level 11, so I need to improvise, but I'm stuck.

所以,到目前为止,我有这样的:

So, far I have this:

            String firstName = contactProperties.get("firstName");
            String lastName = contactProperties.get("lastName");
            String phone = contactProperties.get("phone");
            String email = contactProperties.get("email");
            String company = contactProperties.get("company");
            String postal = contactProperties.get("street") + ", " + contactProperties.get("city") + ", " + contactProperties.get("state") + " " + contactProperties.get("zip") + " " + contactProperties.get("country");

            // Creates a new intent for sending to the device's contacts application
            Intent insertIntent = new Intent(ContactsContract.Intents.Insert.ACTION);

            // Sets the MIME type to the one expected by the insertion activity
            insertIntent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
            insertIntent.putExtra(ContactsContract.Intents.Insert.NAME, firstName + " " + lastName);
            insertIntent.putExtra(ContactsContract.Intents.Insert.EMAIL, email);
            insertIntent.putExtra(ContactsContract.Intents.Insert.PHONE, phone);
            insertIntent.putExtra(ContactsContract.Intents.Insert.COMPANY, company);
            insertIntent.putExtra(ContactsContract.Intents.Insert.POSTAL, postal);

            // Send out the intent to start the device's contacts app in its add contact activity.
            startActivity(insertIntent);

请注意我是如何构成的名称邮政演员。这并不是在所有设备上工作,但因为一些地址簿有姓和名分开场,以及在城市,州和邮编领域。所以,我的解决方案看起来pretty的愚蠢:第一个名称字段第一个和最后一个名字,街道地址字段具有完整的地址。我该如何解决这个问题?会不会有人给我一个例子吗?

Note how I've structured the NAME and POSTAL extras. This doesn't work on all devices, though because some address books have the first and last name fields separated as well as the city, state, and zip fields. So, my "solution" looks pretty stupid: the first name field has both the first and last names, and the street address field has the full address. How can I fix this? Will someone give me an example?

有关的记录,这不工作:

For the record, this doesn't work:

insertIntent.putExtra(ContactsContract.CommonDataKinds.StructuredPostal.STREET, contactProperties.get("street"));
insertIntent.putExtra(ContactsContract.CommonDataKinds.StructuredPostal.CITY, contactProperties.get("city"));

另外请注意,我不希望添加权限在我的清单以允许写入联系人。这是使用意向的关键所在。

Also, note that I don't want to add a permission in my manifest to allow writing to contacts. That's the whole point of using an Intent.

推荐答案

试试这个$ C C $它为我工作。

Try this code it work for me

    String Streetname="ZZZZZ";
 ArrayList < ContentProviderOperation > ops = new ArrayList < ContentProviderOperation > ();
    ops.add(ContentProviderOperation.newInsert(
    ContactsContract.RawContacts.CONTENT_URI)
        .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
        .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
        .build());
   ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Data.MIMETYPE,
        ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
            .withValue(
        ContactsContract.CommonDataKinds.StructuredPostal.STREET,
        Streetname).build());

      try {
        getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (Exception e) {
                  Log.e(getApplicationContext().toString(), "Exception: " + e.getMessage());
    } 

添加到体现

     <uses-permission android:name="android.permission.WRITE_CONTACTS"/>
     <uses-permission android:name="android.permission.READ_CONTACTS"/>

详细信息看到这个帖子。

more details see this post ..

如何在Android的添加新的联系人

这篇关于如何结构化的数据添加到一个新的联系人意向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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