Question

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();
Was it helpful?

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.

OTHER TIPS

Complete answer, including intentUriString for opening the contact with an Intent:

Map getFavoriteContacts(){

    Map contactMap = new HashMap();

    Uri queryUri = ContactsContract.Contacts.CONTENT_URI;

    String[] projection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.STARRED};

    String selection =ContactsContract.Contacts.STARRED + "='1'";

    Cursor cursor = managedQuery(queryUri, projection, selection, null, null);

    while (cursor.moveToNext()) {
        String contactID = cursor.getString(cursor
                .getColumnIndex(ContactsContract.Contacts._ID));

        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri uri = Uri.withAppendedPath(
            ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactID));
        intent.setData(uri);
        String intentUriString = intent.toUri(0);

        String title = (cursor.getString(
            cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));

        contactMap.put(title,intentUriString);
    }

    cursor.close();
    return contactMap;
}

Ty this with Kotlin:

import android.content.Context
import android.provider.ContactsContract
import android.content.Intent
import android.net.Uri


fun getFavoriteContacts(context: Context): Map<*, *> {

    lateinit var contactMap : HashMap<String, String>

    val queryUri = ContactsContract.Contacts.CONTENT_URI.buildUpon()
            .appendQueryParameter(ContactsContract.Contacts.EXTRA_ADDRESS_BOOK_INDEX, "true")
            .build()

    val projection = arrayOf(
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.STARRED
    )

    val selection = ContactsContract.Contacts.STARRED + "='1'"

    val cursor = context.getContentResolver().query(queryUri,
            projection, selection, null, null)

    while (cursor.moveToNext()) {
        val contactID = cursor.getString(cursor
                .getColumnIndex(ContactsContract.Contacts._ID))

        val intent = Intent(Intent.ACTION_VIEW)
        val uri = Uri.withAppendedPath(
                ContactsContract.Contacts.CONTENT_URI, contactID.toString())
        intent.data = uri
        val intentUriString = intent.toUri(0)

        val title = cursor.getString(
                cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))

        contactMap[title] = intentUriString
    }

    cursor.close()
    return contactMap
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top