views:

1966

answers:

3

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.

A: 

On my phone atm, but here is a link with a question similar to yours.

I hope this helps or points you in the right direction.

EDIT:

I had mentioned, I would edit my original answer to at least towards what you were looking for, this is probably wicked late and you have your answer already but here is more information if you were still looking for some, what you need to look for is ContactsContract.CommonDataKinds.StructuredPostal and you can extract specific details regarding address. As for an example using Android 2.0 here shows some examples extracting information from a contact, that is if you haven't figured it out already.

Good luck.

Anthony Forloney
This does not get the address. I can get he name and the phone number already.
Jeremy Edwards
er. my bad, i thought it got the addresses, let me look around and edit my answer
Anthony Forloney
+3  A: 

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.

Nic Strong
A: 

Thanks to Anthony Forloney (below) for the link to "how to read Contacts", that was useful. To extend the content of that link , with the code of how to read "address", these lines should help! They should fetch the postal address, as you see, by querying using the StructuredPostal contants.

            Cursor postals = getContentResolver().query(
                ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
                null,
                ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = "
                        + contactId, null, null);
        int postFormattedNdx = postals.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS);
        int postTypeNdx = postals.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE);
        int postStreetNdx = postals.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET);
        while (postals.moveToNext()) {
            String postalData = postals.getString(postFormattedNdx);
            postalCat = postalCat+ postalData+ ", [";
            postalData = String.valueOf(postals.getInt(postTypeNdx));
            postalCat = postalCat+ postalData+ "], ";
            postalData = postals.getString(postStreetNdx);
            postalCat = postalCat+ postalData+ " ";
        }
        postals.close();

The rest of that link does indeed fetch the name and the phones (when you correct the "has Phone number" mistake for the boolean value--the boolean does not get parsed properly)

Jordan Gee