Android Tutorial

(avery) #1
Android Tutorial 335

Android provides a built-in Contact application, and the contact
data is exposed to other Android applications using the content
provider interface. As an application developer, this means you can
leverage the user’s contact data within your application for a more
robust user experience.

Accessing Private Contact Data

Your application needs special permission to access the private user
information provided by the Contacts content provider. You must
declare a uses-permission tag using the permission
READ_CONTACTS to read this information. The code to start
reading contact data from the Contacts application should look
familiar.

Cursor oneContact = managedQuery( People.CONTENT_URI, null, null,
null, “name desc LIMIT 1”);
Log.d(debugTag, “Count: “ + oneContact.getCount());


This short example simply shows querying for a single contact. We
used LIMIT to retrieve one contact record. If you actually look at
the returned columns of data, you find that there is little more than
the contact name and some indexes. The data fields are not
explicitly returned. Instead, the results include the values needed
to build specific URIs to those pieces of data. We need to request
the data for the contact using these indexes.

Specifically,we retrieve the primary email and primary phone
number for this contact.

int nameIdx = oneContact.getColumnIndex(Contacts.People.NAME);
int emailIDIdx = oneContact
.getColumnIndex(Contacts.People.PRIMARY_EMAIL_ID);

Free download pdf