【问题标题】:Get Android contacts with type-to-filter functionality, restricted to a specific account获取具有类型过滤功能的 Android 联系人,仅限于特定帐户
【发布时间】:2015-08-27 07:25:39
【问题描述】:

我正在尝试:

  • 显示联系人列表
  • 让用户通过键入查询来搜索它们
  • 将搜索结果仅限于特定的 Google/Gmail 帐户。

这就是我为光标构建 URI 的方式:

// 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();

这是最终的 URI:

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

目前,这会显示手机上所有帐户的搜索结果。


注意:如果我使用 Contacts.CONTENT_URI 而不是 Contacts.CONTENT_FILTER_URI,则指定目录/帐户会按预期工作,但我不能再使用“类型过滤”样式搜索。

documentation 确实声明:

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

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

【问题讨论】:

    标签: android android-contentprovider android-contacts contactscontract


    【解决方案1】:

    我在Google's example for contact retrieving 中添加了您的代码,并进行了一些更改,它与我的 Google for Work 帐户完美配合。

    我所做的更改是:

    • 删除带有DIRECTORY_PARAM_KEY 的行,因为我发现它没有任何区别
    • 从 return 语句中删除了 ContactsQuery.SELECTION,因为该常量会阻止显示“不可见”联系人。

    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;
    }
    

    【讨论】:

    • 感谢您的回答,但这似乎有同样的问题。最初,它确实将联系人限制为指定的帐户。但是,一旦您开始输入搜索查询,它就会显示来自所有帐户的联系人。
    • 那么我应该认为这是一个 Android
    • 我实际上是在 Moto X 上运行它(在 Android 5.0 和最近的 5.1 更新上测试)。同样的问题。您是否也让搜索正常工作?
    • 是的,这就是我的意思,搜索等等。你删除了这条线吗? builder.appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(ContactsContract.Directory.DEFAULT));?
    猜你喜欢
    • 1970-01-01
    • 2017-02-07
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 2013-12-16
    相关资源
    最近更新 更多