Android 3.0 - 如何通过ContactsContract检索所有联系人

BVB*_*BVB 13 android google-contacts-api contactscontract android-contacts android-3.0-honeycomb

我正在开发Android Honeycomb(v3.0)应用程序,该应用程序需要显示存储在设备上注册的Google帐户中的所有联系人.我遇到的一个问题是,我只能检索"我的联系人","Android中已加星标"和"其他联系人"中可用的联系人.我还希望能够从"目录"中检索联系人.我认为"目录"部分是Google向希望向其他人提供其域内所有成员/员工目录的组织和公司提供的功能.请看下面的截图:

目录

到目前为止,我的清单文件中有以下行:

<uses-permission android:name="android.permission.READ_CONTACTS" />
Run Code Online (Sandbox Code Playgroud)

我试过使用这段代码:

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();
Run Code Online (Sandbox Code Playgroud)

在我的情况下,"我的联系人"和"在Android中加星标"是空的.但是,获得了"其他联系人"中的(1)联系人.但是,"目录"包含数百个未检索的联系人.

我的问题:有没有办法确保检索"目录"中的联系人?我知道我可以简单地使用网络浏览器复制联系人,然后将它们同步到设备,但如果新的联系人被添加到"目录",我必须每次都手动执行此操作,所以这不是对我来说很棒的选择.请指教.

Ron*_*hta 3

看下面的代码

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++;
                }
            }
Run Code Online (Sandbox Code Playgroud)