我想查询联系人内容提供商,以便联系人的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”。
答案 0 :(得分:0)
为此,您需要查询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();
}
由于
答案 1 :(得分:0)
我偶然发现了同样的问题。原来,该协议应该是Int
而不是String
。如果是自定义的,则应使用ContactsContract.CommonDataKinds.Im.PROTOCOL_CUSTOM
,它是-1
的别名。