Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 929
BVB Avatar asked Jan 24 '12 18:01

BVB


People also ask

What is ContactsContract?

* ContactsContract defines an extensible database of contact-related. * information. Contact information is stored in a three-tier data model: * </p>

How do I select Contacts on Android?

Contact Picker is a feature in android that a developer can use to ask users to select a particular contact. The developer can then query details related to the selected contact and perform the required action.


1 Answers

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++;
                }
            }
like image 85
Ronak Mehta Avatar answered Oct 22 '22 18:10

Ronak Mehta