Get address in contacts using Android SDK

18.8k views Asked by At

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.

4

There are 4 answers

0
Nic Strong On BEST ANSWER

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.

2
Anthony Forloney On

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.

1
Someone Somewhere On

this is how I did it with Android 2.3.3 :

            String where = ContactsContract.Data._ID + " = "
                                                     + uriData.getLastPathSegment()
                                                     + " AND ContactsContract.Data.MIMETYPE = '"
                                                     + ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE
                                                     + "'";

            String[] projection = new String[] { StructuredPostal.STREET,
                                                 StructuredPostal.CITY,
                                                 StructuredPostal.REGION,
                                                 StructuredPostal.POSTCODE,
                                                 StructuredPostal.COUNTRY};

            Cursor cursor = getContentResolver().query( ContactsContract.Data.CONTENT_URI,
                                                         projection,
                                                         where,
                                                         null,
                                                         null);
            String strOutput = "";
            if (cursor.moveToFirst())
            {
                strOutput += "\n" + cursor.getString(0);
                strOutput += "\n" + cursor.getString(1);
                strOutput += "\n" + cursor.getString(2);
                strOutput += "\n" + cursor.getString(3);
                strOutput += "\n" + cursor.getString(4);
            } 

            cursor.close();

NOTE: the uriData is gotten by capturing the intent to view the address. This is solved here: What is the intent associated with "View work address" in Contacts / Contact Card?

Instead of _ID, feel free to try ContactsContract.Data.CONTACT_ID

0
Jordan Gee On

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)