A Simple Suggestion Provider
satya - Thu Dec 06 2012 20:36:29 GMT-0500 (Eastern Standard Time)
Listing 11-1. SimpleSuggestionProvider.java
public class SimpleSuggestionProvider
extends SearchRecentSuggestionsProvider {
final static String AUTHORITY =
"com.androidbook.search.simplesp.SimpleSuggestionProvider";
final static int MODE =
DATABASE_MODE_QUERIES | DATABASE_MODE_2LINES;
public SimpleSuggestionProvider() {
super();
setupSuggestions(AUTHORITY, MODE);
}
}
satya - Thu Dec 06 2012 20:38:05 GMT-0500 (Eastern Standard Time)
android api SearchRecentSuggestionsProvider
satya - Thu Dec 06 2012 20:39:01 GMT-0500 (Eastern Standard Time)
Listing 11-2 Here is how you declare this provider
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=http://schemas.android.com/apk/res/android..>
<application?>
<provider android:name=".SimpleSuggestionProvider"
android:authorities
="com.androidbook.search.simplesp.SimpleSuggestionProvider" />
</application>
</manifest>
satya - Thu Dec 06 2012 20:45:51 GMT-0500 (Eastern Standard Time)
11-4 A search activity that saves recent search suggestions
public class SearchActivity extends Activity
{
private final static String tag ="SearchActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_search_activity);
//this.setDefaultKeyMode(Activity.DEFAULT_KEYS_SEARCH_GLOBAL);
this.setDefaultKeyMode(Activity.DEFAULT_KEYS_SEARCH_LOCAL);
// get and process search query here
final Intent queryIntent = getIntent();
final String queryAction = queryIntent.getAction();
if (Intent.ACTION_SEARCH.equals(queryAction))
{
Log.d(tag,"new intent for search");
this.doSearchQuery(queryIntent);
}
else {
Log.d(tag,"new intent NOT for search");
}
return;
}
@Override
public void onNewIntent(final Intent newIntent)
{
super.onNewIntent(newIntent);
Log.d(tag,"new intent calling me");
// get and process search query here
// Notice we are using the newIntent and not the one
// from the activity.
final Intent queryIntent = newIntent;
final String queryAction = queryIntent.getAction();
if (Intent.ACTION_SEARCH.equals(queryAction))
{
this.doSearchQuery(queryIntent);
Log.d(tag,"new intent for search");
}
else {
Log.d(tag,"new intent NOT for search");
}
}
private void doSearchQuery(final Intent queryIntent)
{
final String queryString =
queryIntent.getStringExtra(SearchManager.QUERY);
// Record the query string in the recent
// queries suggestions provider.
SearchRecentSuggestions suggestions =
new SearchRecentSuggestions(this,
SimpleSuggestionProvider.AUTHORITY,
SimpleSuggestionProvider.MODE);
String helpfullHint = ?SSSP?;
suggestions.saveRecentQuery(queryString, helpfulHint);
}
}
satya - Thu Dec 06 2012 20:49:26 GMT-0500 (Eastern Standard Time)
Listing 11-6. Defining SearchActivity along with its SearchableInfo
<activity android:name=".SearchActivity"
android:label="SSSP: Search Activity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<meta-data android:name="android.app.default_searchable"
android:value=".SearchActivity" />
satya - Thu Dec 06 2012 20:54:54 GMT-0500 (Eastern Standard Time)
Listing 11-7. How to read the query string from a search intent
final String queryString =
queryIntent.getStringExtra(SearchManager.QUERY);
satya - Thu Dec 06 2012 20:55:43 GMT-0500 (Eastern Standard Time)
Here is what else available in the searchmanager api
satya - Thu Dec 06 2012 21:08:36 GMT-0500 (Eastern Standard Time)
Listing 11-9. SimpleSuggestionProvider Search Metadata
<!-- filename: /res/xml/searchable.xml -->
<searchable
xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/search_label"
android:hint="@string/search_hint"
android:searchMode="showSearchLabelAsBadge"
android:queryAfterZeroResults=?true?
android:includeInGlobalSearch="true"
android:searchSuggestAuthority=
"com.androidbook.search.simplesp.SimpleSuggestionProvider"
android:searchSuggestSelection=" ? "
/>
satya - Thu Dec 06 2012 21:09:21 GMT-0500 (Eastern Standard Time)
Discover more attributes for this file here