Вопрос

I have a a custom search suggestions provider. Now, this doesn't really provide suggestion per se but search shortcuts. Here's the custom provider that I've written:

public class QuickSearchProvider extends SearchRecentSuggestionsProvider {

  public final static String AUTHORITY = "com.example.testapp.providers.QuickSearchProvider";
  public final static int MODE = DATABASE_MODE_QUERIES | DATABASE_MODE_2LINES;

  public QuickSearchProvider() {
    setupSuggestions(AUTHORITY, MODE);
  }

  public Cursor query(Uri uri, String[] projection, String sel,
      String[] selArgs, String sortOrder) {

      MatrixCursor cursor = new MatrixCursor(new String[] { 
          BaseColumns._ID,
          SearchManager.SUGGEST_COLUMN_TEXT_1,
          SearchManager.SUGGEST_COLUMN_TEXT_2,
          SearchManager.SUGGEST_COLUMN_ICON_1,
          SearchManager.SUGGEST_COLUMN_INTENT_ACTION});

      cursor.addRow(new Object[] { 0, "Plants", "Search Plants", android.R.drawable.ic_menu_search, Search.SEARCH_PLANTS_ACTION});
      cursor.addRow(new Object[] { 1, "Birds", "Search Birds", android.R.drawable.ic_menu_search, Search.SEARCH_BIRDS_ACTION });

      return new MergeCursor(new Cursor[] { cursor });
  }

}

In my activity which contains the search field, I've written a handler inside the onNewIntent method. Here's an example:

protected void onNewIntent(Intent intent) {
    if  (Search.SEARCH_PLANTS_ACTION.equals(intent.getAction())) {
        ...
    } else if (Search.SEARCH_BIRDS_ACTION.equals(intent.getAction())) {
        ...         
    }
}

As you can see I can easily check which search shortcut was selected but I can't seem to figure out how to get the original query string. Any help?

(On a side note: if you find that my implementation of the custom search suggestion provider is wrong or could be improved, please let me know.)

Thanks.

Это было полезно?

Решение

After a bit of head-banging and trudging through the docs. I've got it!

The search string is always available as the first and only item in the selArgs parameter of the query() method.

In order to get the search string in the intent handler of the activity, you need to use the: intent.getStringExtra(SearchManager.QUERY). By default, this value is only available when a searched is submitted and not when a search suggestion is clicked. If you need to get this value using the custom intent that you've defined for your search suggestion, you need to add it to the cursor like this:

public class QuickSearchProvider extends SearchRecentSuggestionsProvider {

  public final static String AUTHORITY = "com.example.testapp.providers.QuickSearchProvider";
  public final static int MODE = DATABASE_MODE_QUERIES | DATABASE_MODE_2LINES;

  public QuickSearchProvider() {
    setupSuggestions(AUTHORITY, MODE);
  }

  public Cursor query(Uri uri, String[] projection, String sel,
      String[] selArgs, String sortOrder) {

      MatrixCursor cursor = new MatrixCursor(new String[] { 
          BaseColumns._ID,
          SearchManager.SUGGEST_COLUMN_TEXT_1,
          SearchManager.SUGGEST_COLUMN_TEXT_2,
          SearchManager.SUGGEST_COLUMN_ICON_1,
          SearchManager.SUGGEST_COLUMN_QUERY,
          SearchManager.SUGGEST_COLUMN_INTENT_ACTION});

      cursor.addRow(new Object[] { 0, "Plants", "Search Plants", android.R.drawable.ic_menu_search, selArgs[0], Search.SEARCH_PLANTS_ACTION});
      cursor.addRow(new Object[] { 1, "Birds", "Search Birds", android.R.drawable.ic_menu_search, selArgs[0], Search.SEARCH_BIRDS_ACTION });

      return new MergeCursor(new Cursor[] { cursor });
  }

}

Другие советы

Most of the patterns that I have seen call a function called handleIntent() from the onCreate and the onNewIntent() call backs. From there, or within the onNewIntent() the query String gets extracted by doing:

String query = intent.getStringExtra(SearchManager.QUERY);

on the intent. I am not sure if your content provider can be improved or not as I am chasing down the same target right now.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top