How create SearchRecentSuggestionsProvider?

1.3k views Asked by At

My code:

Provider description:

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

But when i run this i don't see my provider. SearchManager searchManager = (SearchManager) (SearchManager)getBaseContext().getApplicationContext().getSystemService(Context.SEARCH_SERVICE); if (searchManager != null) { List searchables = searchManager.getSearchablesInGlobalSearch();

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



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


        }
1

There are 1 answers

0
IgorGanapolsky On

In your searchable activity you need to save each search query in the DB:

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