Search code examples
androidandroid-3.0-honeycombandroid-contactscontactscontractgoogle-contacts-api

Android 3.0 - How to retrieve ALL contacts via ContactsContract


I am working on an Android Honeycomb (v3.0) application that has a requirement of displaying ALL contacts stored within the Google account that is registered on the device. One of the problems I am having is that I can only retrieve the contacts that are available within "My Contacts", "Starred in Android", and "Other Contacts". I would also like to be able to retrieve contacts from the "Directory". I believe that the "Directory" section is a feature provided by Google to organizations and companies who wish to provide a directory of all members/employees within their domains to others. Please see the screenshot below:

Directory

So far, I have the following line in my manifest file:

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

I have tried using this code:

Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
while (cursor.moveToNext()) { 
    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
} 
cursor.close();

In my case, "My Contacts" and "Starred in Android" are empty. However, the (1) contact in "Other Contacts" is obtained. The "Directory" contains hundreds of contacts that are not retrieved, though.

My question: Is there any way to make sure that the contacts in the "Directory" are retrieved as well? I know that I can simply copy the contacts over using the web browser and then sync them to the device, but if a new contact is added to the "Directory", I would have to do this manually every time, so this is not a great choice for me. Please advise.


Solution

  • look at the following code

    import android.app.Activity;
    import android.content.ContentResolver;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.provider.ContactsContract;
              public class TestContacts extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                null, null, null);
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String id = cur.getString(cur
                        .getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur
                        .getString(cur
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
          if (("1")
                        .equals(cur
                                .getString(cur
                                        .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))) {
                    Cursor pCur = cr.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                    + " = ?", new String[] { id }, null);
                    int i = 0;
                    int pCount = pCur.getCount();
                    String[] phoneNum = new String[pCount];
                    String[] phoneType = new String[pCount];
                    while (pCur.moveToNext()) {
                        phoneNum[i] = pCur
                                .getString(pCur
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        phoneType[i] = pCur
                                .getString(pCur
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
    
                        i++;
                    }
                }