在 Android 中获取常用联系人

发布于 2024-11-15 08:37:35 字数 1068 浏览 7 评论 0原文

我正在尝试获取 Android 联系人的收藏夹列表中的所有联系人。目前,我可以获取所有组 ID,包括最喜欢的组 ID。但似乎没有联系人将该群组 ID 作为最喜欢的群组 ID。

我正在尝试获取所有组 ID 和每个组中的联系人。打印两个列表后,发现收藏夹的组id不在联系人列表中

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜雨飘雪 2024-11-22 08:37:35

您可以使用 已加星标 字段。 /developer.android.com/reference/android/provider/ContactsContract.Contacts.html">ContactsContract.Contact 类。如果您将查询更改为:

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

这应该返回 Android 上默认联系人应用程序的“收藏夹”选项卡中显示的所有联系人的列表。

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.

假面具 2024-11-22 08:37:35

完整的答案,包括用于打开带有意图的联系人的intentUriString:

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

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;
}
恬淡成诗 2024-11-22 08:37:35

使用 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
}

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
}
思念绕指尖 2024-11-22 08:37:35

这是在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();
    }

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();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文