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.
Try this code it work for me
add to manifest
more details see this post ..
How to add new contacts in android