androidaddressbookgoogle-contacts-apiimport-contacts

Get address in contacts using Android SDK


I'm trying to retrieve the contact's name, phone number, and address from the android contact list. The name and phone are pretty straight forward but the address seems to not be accessible with the 1.6 api level.

Has someone figured out how to get a contact's address? Also there's a completely new api in 2.0. How can I take advantage of this and fallback to the old api by using 1 binary. If that's even possible.


Solution

  • Prior to Android 2.0 you need to query Contacts.ContactMethods.CONTENT_URI to get the postal addresses you will need to pass

    Contacts.ContactMethods.PERSON_ID + "=? and " + Contacts.ContactMethods.KIND + "=?"
    

    for the selection and for the selectArgs parameter:

    new String[] { personId, Integer.toString(Contacts.KIND_POSTAL) }
    

    As of 2.0 you use the new ContactsContract which is far more complex because a contact can now aggregate information across a number of sources. Some good group postings on the new API are here and here.

    The recommended way to target multiple platform APIs from the same binary is to use reflection (see this blog post). I have found though if you target your app at 1.6 or below in the manifest but set the build path to pick up the Android 2.0 jar file you can call 2.0 apis without the need for reflection. You just need to be VERY careful that classes that are depended on 2.0 classes are not loaded in a 1.6 device or the class loader will throw. To avoid this you will need to use some sort of factory to break the dependencies (or a DI framework like Guice). See this posting for further discussions.