【问题标题】:How create SearchRecentSuggestionsProvider?如何创建 SearchRecentSuggestionsProvider?
【发布时间】:2013-05-14 07:24:20
【问题描述】:

我的代码:

提供者描述:

   <provider android:name=".searchprovider.MySuggestionProvider"
          android:authorities="com.example.music.store.searchprovider.MySuggestionProvider"
          android:exported="false" ></provider>

Java:

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 searchManager = (SearchManager) (SearchManager)getBaseContext().getApplicationContext().getSystemService(Context.SEARCH_SERVICE); 如果(搜索管理器!= null){ 列出可搜索项 = searchManager.getSearchablesInGlobalSearch();

        SearchableInfo info = searchManager.getSearchableInfo(getComponentName());



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


        }

【问题讨论】:

    标签: android provider custom-search-provider


    【解决方案1】:

    在您的可搜索活动中,您需要将每个搜索查询保存在数据库中:

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

    【讨论】:

      猜你喜欢
      • 2015-09-10
      • 2011-06-17
      • 1970-01-01
      • 2020-04-25
      • 2022-01-11
      • 2011-04-02
      • 2016-03-11
      • 2014-09-15
      • 2018-12-08
      相关资源
      最近更新 更多