Toggle Menu

Charlie Calvert on Elvenware

Writing Code and Prose on Computers

Core Code

OS and Tools



Main Page

Contacts API

Uri myPerson = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, 23);
Cursor cur = managedQuery(myPerson, null, null, null, null);

In your manifest, be sure to include the following:

<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

Content Providers

One typically accesses data through content providers. They allow you share data across applications.

There are different content providers for different types of data: audio, video, images, contacts. We will start by exploring contacts.

To get started, you need to call a method of your Activity called getContentResolver that returns an interface that allows you to access the content on your machine. This interface, called ContentResolver, will be the way that you access content.

You run queries that reutnr a Cursorobject that allows you to navigate through a data table.


Uris are used to identify data resources and they look like this:


So you need to know the URI you want, the names of the fields you want, and the types of the fields.


The New Method 

ContactsContract manages the contract between the contacts proveder and applications. Here is where to find information about supported URIs and columns.

There are three different places where contact information is stored:

  • ContactsContract.Data
  • ContactsContract.RawContacts
  • ContractsContract.Contacts

There are other tables, such as Groups, StatusUpdates, AgreegationExceptions, Settngs, SyncState and PhoneLookUp.

Look at this example of how to run a simple query inside a ListActivity:

public void onCreate(Bundle savedInstanceState) 

public void runQuery()
  String[] projection = new String[] 

  // Make the query. 
  Cursor people = managedQuery(ContactsContract.Contacts.CONTENT_URI, 
    projection, // Which columns to return 
    null,       // Which rows to return (all rows) 
    null,       // Selection arguments (none)
    ContactsContract.Contacts.DISPLAY_NAME + " ASC");

  String[] displayFields = new String[] {

  int[] displayViews = new int[] {, 

  SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
    displayFields, displayViews);


The Manifest and Permissions

Don’t forget that you need to update the permissions for your Manifest file so that it includs android.permission.READ_CONTACTS:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
  android:versionName="1.0" >

  <uses-sdk android:minSdkVersion="10" />
  <uses-permission android:name="android.permission.READ_CONTACTS" />
    android:label="@string/app_name" >
    android:label="@string/app_name" >
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />

If you want to write to the contacts provider then you need to ask for write permissions:

<uses-permission android:name="android.permission.WRITE_CONTACTS"/>

Don’t forget that the class in which the above code resides should be a ListActivity, not an Activity:

import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.SimpleCursorAdapter;

public class TestPubCodeActivity extends ListActivity {

The old method

This method is no longer the approved way to display files.

import android.provider.Contacts.People;      
Cursor mCursor = this.getContentResolver().query(People.CONTENT_URI, null, null, null, null);
int[] views = new int[]{};

ListAdapter adapter = new SimpleCursorAdapter(
this, // Context.
new String[] {People.NAME},

// Bind to our new adapter.

Inserting Records

It is now recommended that you use the ContentProviderOperation when inserting records. Here we do not actually specify the account name or type that we want to use, but instead just pass in NULL to get the default account. You can specify a particular account if you want to insert into some other part of the contacts database, for instance, into the GMail list:

private void writeContact(String firstAndLastName) {
    ArrayList ops = new ArrayList();
            .withValue(RawContacts.ACCOUNT_TYPE, null)
            .withValue(RawContacts.ACCOUNT_NAME, null)
            .withValueBackReference(Data.RAW_CONTACT_ID, 0) 
            .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
            .withValue(StructuredName.DISPLAY_NAME, firstAndLastName) 
    try {
        this.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (RemoteException e) {
    } catch (OperationApplicationException e) {

Call the method like this:

writeContact("Sally Chuang");

Don’t forget to ask for write permissions in the manifest:

<uses-permission android:name="android.permission.WRITE_CONTACTS"/>

Copyright © Charlie Calvert | Elvenware Home | Writing Code | Delphi | CSharp | My Books