访问 Android 联系人组名称

发布于 2024-10-05 09:59:47 字数 63 浏览 2 评论 0原文

您能否告诉我如何以编程方式获取联系人组 存储在我们的 Android 手机中?

Can you please tell me how to fetch contact groups programmatically
stored in our android phone?

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

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

发布评论

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

评论(3

吾性傲以野 2024-10-12 09:59:47
final String[] GROUP_PROJECTION = new String[] {
            ContactsContract.Groups._ID, ContactsContract.Groups.TITLE };
    cursor = getContentResolver().query(
    ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION, null,
            null, ContactsContract.Groups.TITLE);

            GlobalConfig.groupList.clear();
    Group g = new Group();
    g.GroupIdList += "0";
    g.setGroupTitle("ALL");
    GlobalConfig.groupList.add(g);
    while (cursor.moveToNext()) {

        String id = cursor.getString(cursor
                .getColumnIndex(ContactsContract.Groups._ID));

        String gTitle = (cursor.getString(cursor
                .getColumnIndex(ContactsContract.Groups.TITLE)));

        if (gTitle.contains("Group:")) {
            gTitle = gTitle.substring(gTitle.indexOf("Group:") + 6).trim();

        }
        if (gTitle.contains("Favorite_")) {
            gTitle = "Favorites";
        }
        if (gTitle.contains("Starred in Android")
                || gTitle.contains("My Contacts")) {
            continue;
        }

        Group gObj = new Group();

        int pos = GlobalConfig.GroupContainsTitle(gTitle);
        if (pos != -1) {
            gObj = GlobalConfig.groupList.get(pos);
            gObj.GroupIdList += "," + id;
            GlobalConfig.groupList.set(pos, gObj);

        } else {
            gObj.GroupIdList += id;
            gObj.setGroupTitle(gTitle);
            GlobalConfig.groupList.add(gObj);

        }

        // Log.d("GrpId  Title", gObj.getGroupIdList() +
        // gObj.getGroupTitle());
    }
final String[] GROUP_PROJECTION = new String[] {
            ContactsContract.Groups._ID, ContactsContract.Groups.TITLE };
    cursor = getContentResolver().query(
    ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION, null,
            null, ContactsContract.Groups.TITLE);

            GlobalConfig.groupList.clear();
    Group g = new Group();
    g.GroupIdList += "0";
    g.setGroupTitle("ALL");
    GlobalConfig.groupList.add(g);
    while (cursor.moveToNext()) {

        String id = cursor.getString(cursor
                .getColumnIndex(ContactsContract.Groups._ID));

        String gTitle = (cursor.getString(cursor
                .getColumnIndex(ContactsContract.Groups.TITLE)));

        if (gTitle.contains("Group:")) {
            gTitle = gTitle.substring(gTitle.indexOf("Group:") + 6).trim();

        }
        if (gTitle.contains("Favorite_")) {
            gTitle = "Favorites";
        }
        if (gTitle.contains("Starred in Android")
                || gTitle.contains("My Contacts")) {
            continue;
        }

        Group gObj = new Group();

        int pos = GlobalConfig.GroupContainsTitle(gTitle);
        if (pos != -1) {
            gObj = GlobalConfig.groupList.get(pos);
            gObj.GroupIdList += "," + id;
            GlobalConfig.groupList.set(pos, gObj);

        } else {
            gObj.GroupIdList += id;
            gObj.setGroupTitle(gTitle);
            GlobalConfig.groupList.add(gObj);

        }

        // Log.d("GrpId  Title", gObj.getGroupIdList() +
        // gObj.getGroupTitle());
    }
淡忘如思 2024-10-12 09:59:47

@Abhi 的答案是好的,但有一些限制:

  • 将列出已删除的联系人
  • 将列出不可见的组
  • 将列出“幽灵”组(即应该已删除但仍处于不确定状态的组)

-

private class GroupInfo {
    String id;
    String title;

    @Override
    public String toString() {
        return title+ " ("+id+")";
    }

    public String getId() {
        return id;
    }
}

List<GroupInfo> groups = new ArrayList<GroupInfo>();

public void loadGroups() {
   final String[] GROUP_PROJECTION = new String[] {
            ContactsContract.Groups._ID, 
            ContactsContract.Groups.TITLE,
            ContactsContract.Groups.SUMMARY_WITH_PHONES
            };

    Cursor c = getContentResolver().query(
            ContactsContract.Groups.CONTENT_SUMMARY_URI,
            GROUP_PROJECTION,
            ContactsContract.Groups.DELETED+"!='1' AND "+
            ContactsContract.Groups.GROUP_VISIBLE+"!='0' "
            ,
            null,
            null);
    final int IDX_ID = c.getColumnIndex(ContactsContract.Groups._ID);
    final int IDX_TITLE = c.getColumnIndex(ContactsContract.Groups.TITLE);

    Map<String,GroupInfo> m = new HashMap<String, GroupInfo>();

    while (c.moveToNext()) {
        GroupInfo g = new GroupInfo();
        g.id = c.getString(IDX_ID);
        g.title = c.getString(IDX_TITLE);
        int users = c.getInt(c.getColumnIndex(ContactsContract.Groups.SUMMARY_WITH_PHONES));
        if (users>0) {
            // group with duplicate name?
            GroupInfo g2 = m.get(g.title);
            if (g2==null) {
                m.put(g.title, g);
                groups.add(g);
            } else {
                g2.id+=","+g.id;
            }
        }
    }
    c.close();
  }

The answer by @Abhi is ok but has some limits:

  • will list deleted contacts
  • will list invisible groups
  • will list 'ghost' groups (that is groups which should have been deleted but are still in the limbo)

-

private class GroupInfo {
    String id;
    String title;

    @Override
    public String toString() {
        return title+ " ("+id+")";
    }

    public String getId() {
        return id;
    }
}

List<GroupInfo> groups = new ArrayList<GroupInfo>();

public void loadGroups() {
   final String[] GROUP_PROJECTION = new String[] {
            ContactsContract.Groups._ID, 
            ContactsContract.Groups.TITLE,
            ContactsContract.Groups.SUMMARY_WITH_PHONES
            };

    Cursor c = getContentResolver().query(
            ContactsContract.Groups.CONTENT_SUMMARY_URI,
            GROUP_PROJECTION,
            ContactsContract.Groups.DELETED+"!='1' AND "+
            ContactsContract.Groups.GROUP_VISIBLE+"!='0' "
            ,
            null,
            null);
    final int IDX_ID = c.getColumnIndex(ContactsContract.Groups._ID);
    final int IDX_TITLE = c.getColumnIndex(ContactsContract.Groups.TITLE);

    Map<String,GroupInfo> m = new HashMap<String, GroupInfo>();

    while (c.moveToNext()) {
        GroupInfo g = new GroupInfo();
        g.id = c.getString(IDX_ID);
        g.title = c.getString(IDX_TITLE);
        int users = c.getInt(c.getColumnIndex(ContactsContract.Groups.SUMMARY_WITH_PHONES));
        if (users>0) {
            // group with duplicate name?
            GroupInfo g2 = m.get(g.title);
            if (g2==null) {
                m.put(g.title, g);
                groups.add(g);
            } else {
                g2.id+=","+g.id;
            }
        }
    }
    c.close();
  }
第七度阳光i 2024-10-12 09:59:47

不需要旧的过分的答案。这里的解决方案要简单得多。

final String[] GROUP_PROJECTION = new String[] {
            ContactsContract.Groups._ID, ContactsContract.Groups.TITLE };
Cursor gC = getContentResolver().query(
            ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION,null,null,null);
gC.moveToFirst();
while (!gC.isAfterLast()) {
        int idcolumn = gC.getColumnIndex(ContactsContract.Groups.TITLE);
        String Id = gC.getString(idcolumn);
        ArrayL.add(Id);
        gC.moveToNext();
}
        LinkedHashSet<String> s = new LinkedHashSet<String>();
        s.addAll(ArrayL);
        ArrayL.clear();
        ArrayL.addAll(s);

No need for old overdone answers. Much simpler solution here.

final String[] GROUP_PROJECTION = new String[] {
            ContactsContract.Groups._ID, ContactsContract.Groups.TITLE };
Cursor gC = getContentResolver().query(
            ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION,null,null,null);
gC.moveToFirst();
while (!gC.isAfterLast()) {
        int idcolumn = gC.getColumnIndex(ContactsContract.Groups.TITLE);
        String Id = gC.getString(idcolumn);
        ArrayL.add(Id);
        gC.moveToNext();
}
        LinkedHashSet<String> s = new LinkedHashSet<String>();
        s.addAll(ArrayL);
        ArrayL.clear();
        ArrayL.addAll(s);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文