首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何创建SearchRecentSuggestionsProvider?

如何创建SearchRecentSuggestionsProvider?
EN

Stack Overflow用户
提问于 2013-05-19 17:48:30
回答 1查看 1.3K关注 0票数 0

我的代码:

提供者说明:

代码语言:javascript
运行
复制
   <provider android:name=".searchprovider.MySuggestionProvider"
          android:authorities="com.example.music.store.searchprovider.MySuggestionProvider"
          android:exported="false" ></provider>

爪哇:

代码语言:javascript
运行
复制
package com.example.music.store.searchprovider;

import android.content.SearchRecentSuggestionsProvider;
import android.database.Cursor;
import android.database.MergeCursor;
import android.net.Uri;
import android.util.Log;

public class MySuggestionProvider extends SearchRecentSuggestionsProvider {
    public final static String AUTHORITY = "com.example.music.store.searchprovider.MySuggestionProvider";
    public final static int MODE = DATABASE_MODE_2LINES | DATABASE_MODE_QUERIES;

    public MySuggestionProvider() {
        super();
        setupSuggestions(AUTHORITY, MODE);
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String sel,
            String[] selArgs, String sortOrder) {
        Cursor recentCursor = super.query(uri, projection, sel, selArgs,
                sortOrder);
        Cursor[] cursors = new Cursor[] { recentCursor, null};

        Log.e("CUR", cursors[0].toString());
        return new MergeCursor(cursors);
        //retrieves a custom suggestion cursor and returns it
    }
}

但是当我运行这个时,我看不到我的提供者。(SearchManager)getBaseContext().getApplicationContext().getSystemService(Context.SEARCH_SERVICE);if (searchManager != null) { List searchables = searchManager.getSearchablesInGlobalSearch();

代码语言:javascript
运行
复制
        SearchableInfo info = searchManager.getSearchableInfo(getComponentName());



        for (SearchableInfo inf : searchables) {
            Log.e("nese",inf.getSuggestAuthority());


        }
EN

回答 1

Stack Overflow用户

发布于 2013-09-16 22:01:55

可搜索活动中,需要将每个搜索查询保存在DB中:

代码语言:javascript
运行
复制
@Override
protected void onNewIntent(Intent intent) {
  setIntent(intent);
  handleIntent(intent);
}

private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        //use the query to search your data somehow
        SearchRecentSuggestions suggestions =
                new SearchRecentSuggestions(this,
                        RecentSuggestionsProvider.AUTHORITY,
                        RecentSuggestionsProvider.MODE);
        suggestions.saveRecentQuery(query, null);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16637618

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档