Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add structured data to a new contact Intent

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.

like image 488
user5243421 Avatar asked Nov 28 '12 22:11

user5243421


1 Answers

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

like image 69
SelvaMariappan Avatar answered Nov 09 '22 20:11

SelvaMariappan