3

I am trying to select a phone number from a global address book (Corporate account). I would like to use the native picker/API because I don't want to prompt the user for login credentials. I came across the ContactsContract.Directory API. However, I couldn't find any samples on how to use it. I tried:

private static final String[] PEOPLE_PROJECTION = new String[] {
    ContactsContract.Directory._ID,
    ContactsContract.Directory.DISPLAY_NAME,
};

StringBuilder buffer = null;
String[] args = null;
if (constraint != null) {
    buffer = new StringBuilder();
    buffer.append("UPPER(");
    buffer.append(Phone.DISPLAY_NAME);
    buffer.append(") GLOB ?");
    args = new String[] { constraint.toString().toUpperCase() + "*" };
}

Cursor c = getContentResolver().query(ContactsContract.Directory.CONTENT_URI, PEOPLE_PROJECTION, buffer == null ? null : buffer.toString(), args, null);

But c always returns null. Please note that I am trying to retrieve just the DISPLAY_NAME here, as I am not sure how to retrieve the phone number yet. Thanks for your help.

radium22
  • 155
  • 1
  • 9

2 Answers2

0

Please look at the source code attached to another question, I asked on SO. The application makes use of ContactsContract.Directory and works on a bunch of different devices, except HTC.
Although the question is very old, perhaps, it will help someone else

Community
  • 1
  • 1
igorepst
  • 1,230
  • 1
  • 12
  • 20
0
Cursor phones = getActivity().getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
            null, null);
    while (phones.moveToNext()) {
        Contact_Class contacts = new Contact_Class();
        contacts.setPersonName(phones.getString(phones
                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
        contacts.setPhoneNumber(phones.getString(phones
                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));

        contact_list.add(contacts);

    }

    phones.close();
public class Contact_Class {
String personName;
String phoneNumber;
public String getPersonName() {
    return personName;
}
public void setPersonName(String personName) {
    this.personName = personName;
}
public String getPhoneNumber() {
    return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
}

}

Pradeep Sodhi
  • 2,135
  • 1
  • 19
  • 19