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

发布于 2024-12-28 12:29:04 字数 937 浏览 2 评论 0原文

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

Directory

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

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

我已尝试使用此代码:

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();

就我而言,“我的联系人”和“Android 中已加星标”为空。然而,获得了“其他联系人”中的(1)联系人。不过,“目录”包含数百个未检索到的联系人。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初吻给了烟 2025-01-04 12:29:04

看下面的代码

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

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++;
                }
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文