<activity
android:name=".app.SearchInvoke"
android:label="@string/search_invoke" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
<!-- 点击候选结果后跳转到哪里 -->
<meta-data
android:name="android.app.default_searchable"
android:value=".app.SearchQueryResults" />
</activity>
SearchInvoke java代码 主要方法
@Override
public boolean onSearchRequested() {
final String queryPrefill = mQueryPrefill.getText().toString();//关键字搜索
Bundle appDataBundle = null;
final String queryAppDataString = mQueryAppData.getText().toString();
if (queryAppDataString != null) {
appDataBundle = new Bundle();
appDataBundle.putString("demo_key", queryAppDataString);//设置开启search activity app的信息
}
// Now call the Activity member function that invokes the Search Manager UI.
startSearch(queryPrefill, false, appDataBundle, false);
// Returning true indicates that we did launch the search, instead of blocking it.
return true;
}
搜索结果后的处理
<activity
android:name=".app.SearchQueryResults"
android:label="@string/search_query_results" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
<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>
/**
* Generic search handler.
*
* In a "real" application, you would use the query string to select results from
* your data source, and present a list of those results to the user.
*/
private void doSearchQuery(final Intent queryIntent, final String entryPoint) {
// The search query is provided as an "extra" string in the query intent
final String queryString = queryIntent.getStringExtra(SearchManager.QUERY);
mQueryText.setText(queryString);
// Record the query string in the recent queries suggestions provider.
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);
suggestions.saveRecentQuery(queryString, null);
final Bundle appData = queryIntent.getBundleExtra(SearchManager.APP_DATA);
if (appData == null) {
mAppDataText.setText("<no app data bundle>");
}
if (appData != null) {
String testStr = appData.getString("demo_key");//前面设置的key
mAppDataText.setText((testStr == null) ? "<no app data>" : testStr);
}
// Report the method by which we were called.
mDeliveredByText.setText(entryPoint);
}
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/search_label"
android:hint="@string/search_hint"//hint时候的文字
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
android:voiceLanguageModel="free_form"
android:voicePromptText="@string/search_invoke"
android:searchSuggestAuthority="com.example.android.apis.SuggestionProvider"
android:searchSuggestSelection=" ? "
/>
更多 参考资料
http://mobile.51cto.com/abased-350777.htm
http://shockwave.iteye.com/blog/737528
在快速搜索框中显示搜索结果
如何在Android Quick Search Box中添加自己的app,按照我的意愿去Search。
http://blog.youkuaiyun.com/yiyaaixuexi/article/details/6218823