13

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.

Janusz
  • 176,216
  • 111
  • 293
  • 365
BVB
  • 5,140
  • 8
  • 37
  • 60
  • http://developer.android.com/resources/samples/ContactManager/index.html – VicVu Jan 24 '12 at 18:37
  • I have just tried the code in the getContacts() function (ContactManager.java file), but there end up being no entries no matter what I set mShowInvisible to. – BVB Jan 24 '12 at 19:11
  • Setting `String selection = null;` returned the same result as I originally had with the code I mentioned. – BVB Jan 24 '12 at 19:35
  • After searching for the answer and not being able to find one, I ended up copying contacts from the directory into "My Contacts". It's a shame that this has to be done manually. – BVB Jan 29 '12 at 08:49

1 Answers1

3

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++;
                }
            }
Ronak Mehta
  • 5,882
  • 5
  • 40
  • 67
  • I think `Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);` will not be able to query the "Directory". I believe I have tried this before. – BVB Feb 15 '12 at 11:12
  • I was not able to find an amicable solution to this problem. A workaround I used was to manually add contacts from the "Directory" to "My Contacts". This would make the code you posted work just fine (mine ended up a little different, but the idea is the same). Of course, if there was a new entry in the "Directory", it would have to be manually moved. I will report back if I find a better way of doing this. – BVB Jun 06 '12 at 09:12
  • @BVB Did you find a better solution in the meantime? – Danilo Bargen Jul 27 '15 at 09:43
  • I have not touched the problem since a couple of years ago and was not able to find a better solution back then, unfortunately. – BVB Jul 27 '15 at 16:20