Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:通过IM获取所有联系人_Android_Android Contentprovider_Android Contacts_Android Cursor - Fatal编程技术网

Android:通过IM获取所有联系人

Android:通过IM获取所有联系人,android,android-contentprovider,android-contacts,android-cursor,Android,Android Contentprovider,Android Contacts,Android Cursor,我想查询联系人内容提供商,以便确定联系人是否具有类型等于“XYZ”的IM 我尝试了以下方法,但没有得到任何结果: Uri uri1 = ContactsContract.Contacts.CONTENT_URI; String[] projection1 = null; String selection1 = null; String[] selectionArgs1 = null; String sortOrder1 = ContactsContract

我想查询联系人内容提供商,以便确定联系人是否具有类型等于“XYZ”的IM

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

    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”。

为此,您需要查询
contacts contract.Data.CONTENT\u URI
,mime类型为IM,然后标签或类型字段(不确定)保存您所说的“XYZ”类型的IM,并且在值列中,您将获得类似用户名的值

此表中有一个外键,它链接到原始联系人表的原始联系人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();
        }

谢谢

我偶然发现了同样的问题。结果表明,协议应该是
Int
而不是
字符串。如果是自定义的,您应该使用
contacts contract.commondatatypes.Im.PROTOCOL\u custom
,这是
-1

的别名,一些代码片段会更有帮助。