使用 CursorLoader 仅查找设备联系人
Posted
技术标签:
【中文标题】使用 CursorLoader 仅查找设备联系人【英文标题】:Find only device contact using CursorLoader 【发布时间】:2016-05-30 05:48:26 【问题描述】:我正在创建一个使用联系人来显示的应用程序。我的问题是我可以显示所有联系人(来自我的电子邮件帐户、Skype 和其他),但我只想显示我设备中的联系人(带有我现在得到的图像),比如打开本机联系人应用程序并转到联系人时 - >菜单->设置->联系人->要显示的联系人->设备联系人。我不太了解联系内容提供商,需要帮助。 *** 上有很多帖子,但我找不到符合我要求的帖子。 为了检索所有联系人,我正在使用这个 CursorLoader
CursorLoader cL = new CursorLoader(getActivity(),
contentUri,
ContactsQuery.PROJECTION,
ContactsQuery.SELECTION,
null,
ContactsQuery.SORT_ORDER
);
这是我的标准和预测:
final static int QUERY_ID = 1;
// A content URI for the Contacts table
final static Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
// The search/filter query Uri
final static Uri FILTER_URI = ContactsContract.Contacts.CONTENT_FILTER_URI;
// The selection clause for the CursorLoader query. The search criteria defined here
// restrict results to contacts that have a display name and are linked to visible groups.
// Notice that the search on the string provided by the user is implemented by appending
// the search string to CONTENT_FILTER_URI.
@SuppressLint("InlinedApi")
final static String SELECTION =
(Utils.hasHoneycomb() ? ContactsContract.Contacts.DISPLAY_NAME_PRIMARY : ContactsContract.Contacts.DISPLAY_NAME) +
"<>''" + " AND " + ContactsContract.Contacts.IN_VISIBLE_GROUP + "= 1 AND "+ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1";
// The desired sort order for the returned Cursor. In android 3.0 and later, the primary
// sort key allows for localization. In earlier versions. use the display name as the sort
// key.
@SuppressLint("InlinedApi")
final static String SORT_ORDER =
Utils.hasHoneycomb() ? ContactsContract.Contacts.SORT_KEY_PRIMARY : ContactsContract.Contacts.DISPLAY_NAME;
// The projection for the CursorLoader query. This is a list of columns that the Contacts
// Provider should return in the Cursor.
@SuppressLint("InlinedApi")
final static String[] PROJECTION =
// The contact's row id
ContactsContract.Contacts._ID,
// A pointer to the contact that is guaranteed to be more permanent than _ID. Given
// a contact's current _ID value and LOOKUP_KEY, the Contacts Provider can generate
// a "permanent" contact URI.
ContactsContract.Contacts.LOOKUP_KEY,
// In platform version 3.0 and later, the Contacts table contains
// DISPLAY_NAME_PRIMARY, which either contains the contact's displayable name or
// some other useful identifier such as an email address. This column isn't
// available in earlier versions of Android, so you must use Contacts.DISPLAY_NAME
// instead.
Utils.hasHoneycomb() ? ContactsContract.Contacts.DISPLAY_NAME_PRIMARY : ContactsContract.Contacts.DISPLAY_NAME,
// In Android 3.0 and later, the thumbnail image is pointed to by
// PHOTO_THUMBNAIL_URI. In earlier versions, there is no direct pointer; instead,
// you generate the pointer from the contact's ID value and constants defined in
// android.provider.ContactsContract.Contacts.
Utils.hasHoneycomb() ? ContactsContract.Contacts.PHOTO_THUMBNAIL_URI : ContactsContract.Contacts._ID,
// The sort order column for the returned Cursor, used by the AlphabetIndexer
SORT_ORDER,
ContactsContract.Contacts.HAS_PHONE_NUMBER
;
【问题讨论】:
【参考方案1】:一段时间后,我将返回搜索此问题的答案。 答案可能不正确,它对我有帮助。
public static boolean getConnections(Context context, String contactId)
// Read all data for contactId
String selection = ContactsContract.RawContacts.CONTACT_ID + " = ?";
String[] selectionArgs = new String[]contactId;
Cursor c = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, selection, selectionArgs, null);
String accountType = null;
while (c.moveToNext())
accountType = c.getString(c.getColumnIndexOrThrow(ContactsContract.RawContacts.ACCOUNT_TYPE));
Log.e("getConnections", accountType + "");
c.close();
if (accountType.equals("SIM Account") || accountType.equals("vnd.sec.contact.phone"))
return true;
else
return false;
在我发布有问题的查询后,我按 contactId 过滤联系人并检查他们的 ACCOUNT_TYPE ,在我的日志中,我在 samsung vnd.sec.contact.phone(表示这是电话联系人)和我的 lg 上看到 2 sim 我发现此 SIM 帐户。 也许有更好的解决方案,但我是这样想的。
【讨论】:
以上是关于使用 CursorLoader 仅查找设备联系人的主要内容,如果未能解决你的问题,请参考以下文章
使用 CursorLoader 显示具有多个电话号码的联系人
Xamarin Android,使用带有选择和选择参数的 CursorLoader 获取联系人手机号码
SQLite 异常错误,使用 CursorLoader 和 ContactContracts