【问题标题】:Android: get all contacts by IMAndroid:通过 IM 获取所有联系人
【发布时间】:2017-02-22 03:10:18
【问题描述】:

我想查询联系人内容提供商,如果联系人的 IM 类型等于“XYZ”。

我尝试了以下方法,但没有得到任何结果:

    Uri uri1 = ContactsContract.Contacts.CONTENT_URI;
    String[] projection1 = null;
    String selection1 = null;
    String[] selectionArgs1 = null;
    String sortOrder1 = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC";
    Cursor cursor1 = context.getContentResolver().query(uri1, projection1, selection1, selectionArgs1, sortOrder1);
    if (cursor1 != null && cursor1.getCount() > 0) {
        while (cursor1.moveToNext()) {
            int contactId = Integer.parseInt(cursor1.getString(cursor1.getColumnIndex(ContactsContract.Contacts._ID)));

            Uri uri2 = ContactsContract.Data.CONTENT_URI;
            String[] projection2 = null;
            String selection2 = ContactsContract.CommonDataKinds.Im.PROTOCOL + " = ? AND " + ContactsContract.Contacts._ID + " = ? ";
            String[] selectionArgs2 = new String[]{"XYZ", contactId + ""};
            String sortOrder2 = null;
            Cursor cursor2 = context.getContentResolver().query(uri2, projection2, selection2, selectionArgs2, sortOrder2);
            if (cursor2 != null && cursor2.getCount() > 0) {
                while (cursor2.moveToNext()) {
                    Log.i(TAG, "Name: " + cursor2.getString(cursor2.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)));

                }
                DatabaseUtils.dumpCursor(cursor2);
            }
        }
        cursor1.close();
    }

上面的代码我没有得到任何日志。

PS:我没有使用 AIM、Windows Live、Yahoo 或 Skype 等内置协议。它是我的自定义协议,说它“XYZ”。

【问题讨论】:

    标签: android android-contentprovider android-contacts android-cursor


    【解决方案1】:

    为此,您需要查询ContactsContract.Data.CONTENT_URI,并将 mime 类型设为 IM,然后标签或类型字段(不确定)包含您所说的 'XYZ' 的 IM 类型以及您在值列中将获得像用户名一样的值。

    此表中有一个外键链接到 raw_contacts 表的原始联系人 ID。

    更新

    Cursor cursor = getActivity().getApplicationContext().getContentResolver().query(
                    ContactsContract.Data.CONTENT_URI, null, ContactsContract.Data.MIMETYPE + "=?", new String[]{ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE}, null);
            if(cursor!=null) {
                cursor.moveToFirst();
                do {
                    String value = cursor
                            .getString(cursor
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));
    
                    //Types are defined in CommonDataKinds.Im.*
                    int imppType = cursor
                            .getInt(cursor
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE));
    
                    //Protocols are defined in CommonDataKinds.Im.*
                    int imppProtocol = cursor
                            .getInt(cursor
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Im.PROTOCOL));
                    //and in this protocol you can check your custom value
                }while (cursor.moveToNext());
                cursor.close();
            }
    

    谢谢

    【讨论】:

    • 一些 sn-p 会更有帮助。
    【解决方案2】:

    我偶然发现了同样的问题。结果表明协议应该是Int 而不是String。如果是自定义的,您应该使用ContactsContract.CommonDataKinds.Im.PROTOCOL_CUSTOM,它是-1 的别名。

    【讨论】:

      猜你喜欢
      • 2012-09-15
      • 2018-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-23
      • 1970-01-01
      相关资源
      最近更新 更多