Tuesday, May 8, 2012

How can I choose a phone number with Android"s contacts dialog


I'm using the old Contacts API to choose a contact with a phone number. I want to use the newer ContactsContracts API. I want...



  1. ...a dialog shown with all contacts that have phone numbers.

  2. ...the user to choose a contact AND one of their phone numbers.

  3. ...access to the chosen phone number.



The ContactsContracts is very complicated. I found many examples, but none that fit my needs. I don't want to choose a contact and then query for the contact's details because this will give me a list of their phone numbers. I need the user to choose ONE of the contact's phone numbers. I don't want to have to write my own dialogs to display the contacts or to have the user pick a phone number. Is there any simple way to get what I want?



Here is the old API code I'm using:




static public final int CONTACT = 0;
...
Intent intent = new Intent(Intent.ACTION_PICK, Contacts.Phones.CONTENT_URI);
startActivityForResult(intent, CONTACT);
...
public void onActivityResult (int requestCode, int resultCode, Intent intent) {
if (resultCode != Activity.RESULT_OK || requestCode != CONTACT) return;
Cursor c = managedQuery(intent.getData(), null, null, null, null);
if (c.moveToFirst()) {
String phone = c.getString(c.getColumnIndexOrThrow(Contacts.Phones.NUMBER));
// yay
}
}


Source: Tips4all

No comments:

Post a Comment