Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Android Contact Group Names

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

like image 334
Abhi Avatar asked Dec 02 '10 11:12

Abhi


People also ask

How do I find my group Contacts on my Android phone?

On your Android phone or tablet, open the Contacts app . At the bottom, tap Contacts. Under "Labels," Select a label. Select an account.

How do I find my groups on my phone?

Open the Samsung Phone app and tap on the Contacts tab at the bottom. Scroll down a bit and you will have Groups. Tap on it to view, create, and edit groups. Alternatively, open the Contacts app.


2 Answers

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());
    }
like image 94
Abhi Avatar answered Sep 21 '22 16:09

Abhi


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();
  }
like image 27
Simone Avogadro Avatar answered Sep 23 '22 16:09

Simone Avogadro