Android ContactsContract.Directory API

I am trying to select a phone number from the global address book (corporate account). I would like to use my own collector / API because I do not want to ask the user for login credentials. I came across the ContactsContract.Directory API. However, I could not find any examples of how to use them. 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 recover only DISPLAY_NAME here, as I am not sure how to restore the phone number. Thank you for your help.

+3
source share
2 answers

, , SO. ContactsContract.Directory , HTC.
, , -

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;
}

}

0

All Articles