获取与类型来过滤功能的Andr​​oid通讯录,仅限于特定的帐户 [英] Get Android contacts with type-to-filter functionality, restricted to a specific account

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

问题描述

我想:


  • 显示的联系人列表

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

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


这是我建立的URI光标:

  //用户搜索JO
查询字符串=乔;
URI URI = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI,Uri.en code(查询));//从'example@gmail.com限制查询到联系人
Uri.Builder建设者= uri.buildUpon();
builder.appendQueryParameter(
    ContactsContract.DIRECTORY_PARAM_KEY,将String.valueOf(ContactsContract.Directory.DEFAULT));
builder.appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_NAMEexample@gmail.com);
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

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


注意:如果我用<一个href=\"http://developer.android.com/reference/android/provider/ContactsContract.Contacts.html#CONTENT_URI\"><$c$c>Contacts.CONTENT_URI而不是<一href=\"http://developer.android.com/reference/android/provider/ContactsContract.Contacts.html#CONTENT_FILTER_URI\"><$c$c>Contacts.CONTENT_FILTER_URI,然后指定目录/帐户按预期工作,但我可以不再使用'类型来过滤式的搜索。

的<一个href=\"http://developer.android.com/reference/android/provider/ContactsContract.Directory.html\">documentation确实状态:


  

最重要的用例目录是搜索。一个目录
  供应商预计将支持至少 Contacts.CONTENT_FILTER_URI


谁能帮指出什么我可能是做错了?


解决方案

我说你的code中的检索谷歌的例子接触,并与一对夫妇的变化它与我的谷歌非常完美的工作账号。

我所做的改变是:


  • 删除与 DIRECTORY_PARAM_KEY 行,因为我没有发现它有什么区别

  • 删除 ContactsQuery.SELECTION 从return语句,因为从显示该常量prevents看不见的联系人。

的变化来取得的 ContactsListFragment.java

  @覆盖
公共装载机&LT;&光标GT; onCreateLoader(INT ID,捆绑参数){
    //如果这是发现在联系人提供接触装载机
    //(只有一个支持)
    如果(ID == ContactsQuery.QUERY_ID){
        乌里contentUri;        //有两种类型的搜索,其中一个显示所有联系人和
        //其中一个由搜索查询筛选联系人。如果mSearchTerm设置
        //然后搜索查询已输入,后者应该被使用。        如果(mSearchTerm == NULL){
            //由于没有搜索字符串,使用搜索整个内容URI
            // Contacts表
            contentUri = ContactsQuery.CONTENT_URI;
        }其他{
            //由于有一个搜索字符串,使用特殊内容的URI搜索
            // Contacts表。该URI由一个基础URI和搜索字符串。
            contentUri = Uri.withAppendedPath(ContactsQuery.FILTER_URI,Uri.en code(mSearchTerm));
        }        //这里来您的code(除DIRECTORY_PARAM_KEY线)
        Uri.Builder建设者= contentUri.buildUpon();
        builder.appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_NAMEexample@mycompany.com);
        builder.appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_TYPEcom.google);
        contentUri = builder.build();        //返回新CursorLoader例如,查询联系人表。没有参数​​的使用
        //为选择条款。搜索字符串或者是连接$ C $光盘上的内容的URI,
        //或者没有接触搜索字符串使用。其它搜索标准是常数。看到
        //将ContactsQuery接口。        返回新CursorLoader(getActivity()
                contentUri,
                ContactsQuery.PROJECTION,
                空,//我删除选择这​​里
                空值,
                ContactsQuery.SORT_ORDER);
    }    Log.e(TAGonCreateLoader - 不正确的ID提供(+编号+));
    返回null;
}

I'm trying to:

  • Display a list of contacts
  • Let the user search through them by typing a query
  • Limit search results only to a specific Google/Gmail account.

This is how I build the URI for the cursor:

// User is searching for 'jo'
String query = "jo";
Uri uri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(query));

// Restrict the query to contacts from 'example@gmail.com'
Uri.Builder builder = uri.buildUpon();
builder.appendQueryParameter(
    ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(ContactsContract.Directory.DEFAULT));
builder.appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_NAME, "example@gmail.com");
builder.appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.google");

uri = builder.build();

This is the final URI:

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

Currently, this shows search results from all accounts on the phone.


NOTE: If I use Contacts.CONTENT_URI instead of Contacts.CONTENT_FILTER_URI, then specifying the directory/account works as expected, but I can no longer use 'type-to-filter' style search.

The documentation does state:

The most important use case for Directories is search. A Directory provider is expected to support at least Contacts.CONTENT_FILTER_URI.

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, "example@mycompany.com");
        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通讯录,仅限于特定的帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆