Programming Android, 2nd Edition
by Zigurd Mednieks, Laird Dornin, G. Blake Meike, Masumi Nakamura
Chapter 18. Communication, Identity, Sync, and Social Media
One of the primary data types that is stored and used (and reused) in Android is contact data. This consists of the various pieces of information associated with a contact—name, phone number, email, and so on. In Android 2.0 (API level 5), contact data was significantly expanded (allowing access to multiple accounts and support for aggregation of similar contacts). In earlier chapters we covered the use of content providers and Android database classes, so we will not cover that preliminary material in this chapter. Instead, we will focus on the use of the ContactsContract content provider.
Account Contacts
To access the account contacts the following permissions must be provided in the manifest:
<uses-permissionandroid:name="android.permission.GET_ACCOUNTS"/><uses-permissionandroid:name="android.permission.READ_CONTACTS"/><uses-permissionandroid:name="android.permission.WRITE_CONTACTS"/>
Within an Activity, we can use the managedQuery method to query the ContactsContract.Contacts data and return a
Cursor for our use:
privateCursorgetContacts(){Uriuri=ContactsContract.Contacts.CONTENT_URI;String[]projection=newString[]{ContactsContract.Contacts._ID,ContactsContract.Contacts.LOOKUP_KEY,ContactsContract.Contacts.DISPLAY_NAME};Stringselection=null;String[]selectionArgs=null;StringsortOrder=ContactsContract.Contacts.DISPLAY_NAME+" COLLATE LOCALIZED ASC";returnmanagedQuery(uri,projection ...