androidcontactsaddressbookfavorites

Getting favourites contacts in Android


I am trying to get all contacts in the favourites list of the Android contacts. Currently, I can get all the group ids including the favourite group ID. But it seems that there is no contacts that have the group ID as the favourite group ID.

I'm trying to get All groups id and contacts in each group. After printing two list, I found that the group id of favorite is not in the contact list

ArrayList<String> favGroupId=new ArrayList<String>();
        final String[] GROUP_PROJECTION = new String[] {
                ContactsContract.Groups._ID, ContactsContract.Groups.TITLE };
        Cursor  cursor = getContentResolver().query(
        ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION, null,
                null, ContactsContract.Groups.TITLE);

        while (cursor.moveToNext()) {
            String id = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.Groups._ID));
            Log.v("Test",id);

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

            Log.v("Test",gTitle);
            if (gTitle.contains("Favorite_")) {
                gTitle = "Favorites";
                favGroupId.add(id);
            }
        }
        cursor.close();

Solution

  • You can use the STARRED field in the ContactsContract.Contact class. If you change your query to:

    Cursor cursor = this.managedQuery(
        ContactsContract.Contacts.CONTENT_URI, projection, "starred=?",
        new String[] {"1"}, null);
    

    this should return a list of all contacts that appear in the Favorites tab in the default Contacts app on Android.