Look Inside Yourself
Step #3: Find the Correct Contact Uri
Since we want this particular code to run on a variety of Android releases,
we need to figure out the right Uri to use to wrap in an ACTION_PICK Intent
to let the user pick a contact. In Android 2.0 and newer, the Uri is
android.provider.ContactsContract.Contacts.CONTENT_URI, while in Android
1.6 and earlier, the Uri is android.provider.Contacts.People.CONTENT_URI.
Fortunately, this value will not change during the execution of our
application, so we can find out the right value via a static initializer, then
use the value throughout the application.
Hence, add the following static data member and initializer block to
Contacter, along with the required imports:
private static Uri CONTENT_URI=null;
static {
int sdk=new Integer(Build.VERSION.SDK).intValue();
if (sdk>= 5 ) {
try {
Class clazz=Class.forName("android.provider.ContactsContract$Contacts");
CONTENT_URI=(Uri)clazz.getField("CONTENT_URI").get(clazz);
}
catch (Throwable t) {
Log.e("PickDemo", "Exception when determining CONTENT_URI", t);
}
}
else {
CONTENT_URI=Contacts.People.CONTENT_URI;
}
}
Step #4: Attach the Button to the Contacts
Next, we want to arrange to let the user pick a contact when the button is
clicked.
To do this, add the following implementation of onCreate() to Contacter: