Getting favourites contacts in Android
Asked Answered
T

4

14

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();
Tours answered 15/6, 2011 at 0:9 Comment(0)
S
24

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.

Silvereye answered 15/6, 2011 at 3:44 Comment(0)
I
5

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;
}
Issue answered 19/9, 2013 at 10:47 Comment(0)
P
2

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
}
Preordain answered 2/4, 2018 at 4:5 Comment(0)
N
1

Here is the complete method to get contact details with favorite in Java

ContentResolver contentResolver = context.getContentResolver();

    if (contentResolver == null)
        return;

    String[] fieldListProjection = {
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY,
            ContactsContract.CommonDataKinds.Phone.NUMBER,
            ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER,
            ContactsContract.Contacts.HAS_PHONE_NUMBER,
            ContactsContract.Contacts.PHOTO_URI
            ,ContactsContract.Contacts.STARRED
    };
    String sort = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY + " ASC";
    Cursor phones = contentResolver
            .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI
                    , fieldListProjection, null, null, sort);
    HashSet<String> normalizedNumbersAlreadyFound = new HashSet<>();

    if (phones != null && phones.getCount() > 0) {
        while (phones.moveToNext()) {
            String normalizedNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

            if (Integer.parseInt(phones.getString(phones.getColumnIndex(
                    ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                if (normalizedNumbersAlreadyFound.add(normalizedNumber)) {

                    int id = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
                    String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                    int fav = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.STARRED));
                    boolean isFav;
                    isFav= fav == 1;

                    String uri = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
                    if(uri!=null){
                        contactList.add(new FavContact(id,isFav,name,phoneNumber,uri));
                    }
                    else{
                        contactList.add(new FavContact(id,isFav,name,phoneNumber));
                    }

                }
            }
        }
        phones.close();
    }
Nipping answered 18/9, 2021 at 11:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.