How to add structured data to a new contact Intent

2019-04-06 15:08发布

I need to support Android 2.1 and up.

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

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.

1条回答
手持菜刀,她持情操
2楼-- · 2019-04-06 15:22

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

add to manifest

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

more details see this post ..

How to add new contacts in android

查看更多
登录 后发表回答