How to add structured data to a new contact Intent
Asked Answered
G

1

8

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.

Gaulish answered 28/11, 2012 at 22:41 Comment(1)
Do you have separate code for inserting in Contacts for API 11 and above and API level 11 and below?Aldarcy
A
1

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

Aswan answered 4/1, 2013 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.