Getting favourites contacts in Android

三世轮回 提交于 2019-12-17 10:29:25

问题


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();

回答1:


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.




回答2:


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;
}



回答3:


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
}


来源:https://stackoverflow.com/questions/6351626/getting-favourites-contacts-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!