本文介绍了获取与类型来过滤功能的Andr​​oid通讯录,仅限于特定的帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想:


  • 显示的联系人列表

  • 让通过他们的用户搜索中键入查询

  • 限制搜索结果仅与特定的谷歌/ Gmail帐户。


这是我建立的URI光标:

  //用户搜索JO
查询字符串=乔;
URI URI = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI,Uri.en code(查询));//从'[email protected]限制查询到联系人
Uri.Builder建设者= uri.buildUpon();
builder.appendQueryParameter(
    ContactsContract.DIRECTORY_PARAM_KEY,将String.valueOf(ContactsContract.Directory.DEFAULT));
builder.appendQueryParameter([email protected]);
builder.appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_TYPEcom.google);的uri = builder.build();

这是最后的URI:

<$p$p><$c$c>content://com.android.contacts/contacts/filter/jo?directory=0&account_name=example%40gmail.com&account_type=com.google

目前,这表明从的所有的账户在手机上的搜索结果。


注意:如果我用<$c$c>Contacts.CONTENT_URI而不是<$c$c>Contacts.CONTENT_FILTER_URI,然后指定目录/帐户按预期工作,但我可以不再使用'类型来过滤式的搜索。

的documentation确实状态:

Could anyone help point out what I might be doing wrong?

解决方案

I added your code in Google's example for contact retrieving, and with a couple of changes it worked perfectly with my Google for Work account.

The changes I made were:

  • remove the line with DIRECTORY_PARAM_KEY, as I didn't find it to make any difference
  • removed ContactsQuery.SELECTION from the return statement, because that constant prevents "invisible" contacts from being displayed.

The changes were made to ContactsListFragment.java

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    // If this is the loader for finding contacts in the Contacts Provider
    // (the only one supported)
    if (id == ContactsQuery.QUERY_ID) {
        Uri contentUri;

        // There are two types of searches, one which displays all contacts and
        // one which filters contacts by a search query. If mSearchTerm is set
        // then a search query has been entered and the latter should be used.

        if (mSearchTerm == null) {
            // Since there's no search string, use the content URI that searches the entire
            // Contacts table
            contentUri = ContactsQuery.CONTENT_URI;
        } else {
            // Since there's a search string, use the special content Uri that searches the
            // Contacts table. The URI consists of a base Uri and the search string.
            contentUri = Uri.withAppendedPath(ContactsQuery.FILTER_URI, Uri.encode(mSearchTerm));
        }

        // HERE COMES YOUR CODE (except the DIRECTORY_PARAM_KEY line)
        Uri.Builder builder = contentUri.buildUpon();
        builder.appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_NAME, "[email protected]");
        builder.appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.google");
        contentUri = builder.build();

        // Returns a new CursorLoader for querying the Contacts table. No arguments are used
        // for the selection clause. The search string is either encoded onto the content URI,
        // or no contacts search string is used. The other search criteria are constants. See
        // the ContactsQuery interface.

        return new CursorLoader(getActivity(),
                contentUri,
                ContactsQuery.PROJECTION,
                null, // I REMOVED SELECTION HERE
                null,
                ContactsQuery.SORT_ORDER);
    }

    Log.e(TAG, "onCreateLoader - incorrect ID provided (" + id + ")");
    return null;
}

这篇关于获取与类型来过滤功能的Andr​​oid通讯录,仅限于特定的帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 00:47