下面列出了怎么用android.app.SearchableInfo的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Sets the SearchableInfo for this SearchView. Properties in the SearchableInfo are used
* to display labels, hints, suggestions, create intents for launching search results screens
* and controlling other affordances such as a voice button.
*
* @param searchable a SearchableInfo can be retrieved from the SearchManager, for a specific
* activity or a global search provider.
*/
public void setSearchableInfo(SearchableInfo searchable) {
mSearchable = searchable;
if (mSearchable != null) {
updateSearchAutoComplete();
updateQueryHint();
}
// Cache the voice search capability
mVoiceButtonEnabled = hasVoiceSearch();
if (mVoiceButtonEnabled) {
// Disable the microphone on the keyboard, as a mic is displayed near the text box
// TODO: use imeOptions to disable voice input when the new API will be available
mSearchSrcTextView.setPrivateImeOptions(IME_OPTION_NO_MICROPHONE);
}
updateViewsVisibility(isIconified());
}
/**
* Handles the key down event for dealing with action keys.
*
* @param keyCode This is the keycode of the typed key, and is the same value as
* found in the KeyEvent parameter.
* @param event The complete event record for the typed key
*
* @return true if the event was handled here, or false if not.
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (mSearchable == null) {
return false;
}
// if it's an action specified by the searchable activity, launch the
// entered query with the action key
SearchableInfo.ActionKeyInfo actionKey = mSearchable.findActionKey(keyCode);
if ((actionKey != null) && (actionKey.getQueryActionMsg() != null)) {
launchQuerySearch(keyCode, actionKey.getQueryActionMsg(), mSearchSrcTextView.getText()
.toString());
return true;
}
return super.onKeyDown(keyCode, event);
}
/**
* For a given suggestion and a given cursor row, get the action message. If
* not provided by the specific row/column, also check for a single
* definition (for the action key).
*
* @param c The cursor providing suggestions
* @param actionKey The actionkey record being examined
*
* @return Returns a string, or null if no action key message for this
* suggestion
*/
private static String getActionKeyMessage(Cursor c, SearchableInfo.ActionKeyInfo actionKey) {
String result = null;
// check first in the cursor data, for a suggestion-specific message
final String column = actionKey.getSuggestActionMsgColumn();
if (column != null) {
result = SuggestionsAdapter.getColumnString(c, column);
}
// If the cursor didn't give us a message, see if there's a single
// message defined
// for the actionkey (for all suggestions)
if (result == null) {
result = actionKey.getSuggestActionMsg();
}
return result;
}
private void onVoiceClicked() {
// guard against possible race conditions
if (mSearchable == null) {
return;
}
SearchableInfo searchable = mSearchable;
try {
if (searchable.getVoiceSearchLaunchWebSearch()) {
Intent webSearchIntent = createVoiceWebSearchIntent(mVoiceWebSearchIntent,
searchable);
getContext().startActivity(webSearchIntent);
} else if (searchable.getVoiceSearchLaunchRecognizer()) {
Intent appSearchIntent = createVoiceAppSearchIntent(mVoiceAppSearchIntent,
searchable);
getContext().startActivity(appSearchIntent);
}
} catch (ActivityNotFoundException e) {
// Should not happen, since we check the availability of
// voice search before showing the button. But just in case...
Log.w(LOG_TAG, "Could not find voice search activity");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// Inflate the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Use the Search Manager to find the SearchableInfo related
// to the Search Result Activity.
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo = searchManager.getSearchableInfo(
new ComponentName(getApplicationContext(),
EarthquakeSearchResultActivity.class));
SearchView searchView =
(SearchView)menu.findItem(R.id.search_view).getActionView();
searchView.setSearchableInfo(searchableInfo);
searchView.setIconifiedByDefault(false);
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// Inflate the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Use the Search Manager to find the SearchableInfo related
// to the Search Result Activity.
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo = searchManager.getSearchableInfo(
new ComponentName(getApplicationContext(),
EarthquakeSearchResultActivity.class));
SearchView searchView =
(SearchView)menu.findItem(R.id.search_view).getActionView();
searchView.setSearchableInfo(searchableInfo);
searchView.setIconifiedByDefault(false);
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// Inflate the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Use the Search Manager to find the SearchableInfo related
// to the Search Result Activity.
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo = searchManager.getSearchableInfo(
new ComponentName(getApplicationContext(),
EarthquakeSearchResultActivity.class));
SearchView searchView =
(SearchView)menu.findItem(R.id.search_view).getActionView();
searchView.setSearchableInfo(searchableInfo);
searchView.setIconifiedByDefault(false);
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// Inflate the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Use the Search Manager to find the SearchableInfo related
// to the Search Result Activity.
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo = searchManager.getSearchableInfo(
new ComponentName(getApplicationContext(),
EarthquakeSearchResultActivity.class));
SearchView searchView =
(SearchView)menu.findItem(R.id.search_view).getActionView();
searchView.setSearchableInfo(searchableInfo);
searchView.setIconifiedByDefault(false);
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// Inflate the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Use the Search Manager to find the SearchableInfo related
// to the Search Result Activity.
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo = searchManager.getSearchableInfo(
new ComponentName(getApplicationContext(),
EarthquakeSearchResultActivity.class));
SearchView searchView =
(SearchView)menu.findItem(R.id.search_view).getActionView();
searchView.setSearchableInfo(searchableInfo);
searchView.setIconifiedByDefault(false);
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// Inflate the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Use the Search Manager to find the SearchableInfo related
// to the Search Result Activity.
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo = searchManager.getSearchableInfo(
new ComponentName(getApplicationContext(),
EarthquakeSearchResultActivity.class));
SearchView searchView =
(SearchView)menu.findItem(R.id.search_view).getActionView();
searchView.setSearchableInfo(searchableInfo);
searchView.setIconifiedByDefault(false);
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
// Use the Search Manager to find the SearchableInfo related
// to this Activity.
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo =
searchManager.getSearchableInfo(getComponentName());
SearchView searchView =
(SearchView) menu.findItem(R.id.search_view).getActionView();
searchView.setSearchableInfo(searchableInfo);
searchView.setIconifiedByDefault(false);
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// Inflate the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Use the Search Manager to find the SearchableInfo related
// to the Search Result Activity.
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo = searchManager.getSearchableInfo(
new ComponentName(getApplicationContext(),
EarthquakeSearchResultActivity.class));
SearchView searchView =
(SearchView)menu.findItem(R.id.search_view).getActionView();
searchView.setSearchableInfo(searchableInfo);
searchView.setIconifiedByDefault(false);
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// Inflate the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Use the Search Manager to find the SearchableInfo related
// to the Search Result Activity.
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo = searchManager.getSearchableInfo(
new ComponentName(getApplicationContext(),
EarthquakeSearchResultActivity.class));
SearchView searchView =
(SearchView)menu.findItem(R.id.search_view).getActionView();
searchView.setSearchableInfo(searchableInfo);
searchView.setIconifiedByDefault(false);
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// Inflate the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Use the Search Manager to find the SearchableInfo related
// to the Search Result Activity.
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo = searchManager.getSearchableInfo(
new ComponentName(getApplicationContext(),
EarthquakeSearchResultActivity.class));
SearchView searchView =
(SearchView)menu.findItem(R.id.search_view).getActionView();
searchView.setSearchableInfo(searchableInfo);
searchView.setIconifiedByDefault(false);
return true;
}
/**
* Sets the SearchableInfo for this SearchView. Properties in the SearchableInfo are used
* to display labels, hints, suggestions, create intents for launching search results screens
* and controlling other affordances such as a voice button.
*
* @param searchable a SearchableInfo can be retrieved from the SearchManager, for a specific
* activity or a global search provider.
*/
public void setSearchableInfo(SearchableInfo searchable) {
mSearchable = searchable;
if (mSearchable != null) {
updateSearchAutoComplete();
updateQueryHint();
}
// Cache the voice search capability
mVoiceButtonEnabled = hasVoiceSearch();
if (mVoiceButtonEnabled) {
// Disable the microphone on the keyboard, as a mic is displayed near the text box
// TODO: use imeOptions to disable voice input when the new API will be available
mQueryTextView.setPrivateImeOptions(IME_OPTION_NO_MICROPHONE);
}
updateViewsVisibility(isIconified());
}
private void onVoiceClicked() {
// guard against possible race conditions
if (mSearchable == null) {
return;
}
SearchableInfo searchable = mSearchable;
try {
if (searchable.getVoiceSearchLaunchWebSearch()) {
Intent webSearchIntent = createVoiceWebSearchIntent(mVoiceWebSearchIntent,
searchable);
getContext().startActivity(webSearchIntent);
} else if (searchable.getVoiceSearchLaunchRecognizer()) {
Intent appSearchIntent = createVoiceAppSearchIntent(mVoiceAppSearchIntent,
searchable);
getContext().startActivity(appSearchIntent);
}
} catch (ActivityNotFoundException e) {
// Should not happen, since we check the availability of
// voice search before showing the button. But just in case...
Log.w(LOG_TAG, "Could not find voice search activity");
}
}
void onFinishSetupOptionsMenu(final Menu menu) {
searchItem = menu.findItem(R.id.menu_global_search);
if (searchItem != null) {
searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
SearchManager searchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo = searchManager.getSearchableInfo(context.getComponentName());
if (searchableInfo == null) {
Log.w(TAG, "Failed to get SearchableInfo");
} else {
searchView.setSearchableInfo(searchableInfo);
}
String currentQuery = getCurrentQuery();
if (currentQuery != null) {
searchView.setOnSearchClickListener(v -> searchView.setQuery(getCurrentQuery(), false));
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem item = menu.findItem(R.id.action_search);
searchView = (SearchView) MenuItemCompat.getActionView(item);
// 关联检索配置与 SearchActivity
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo = searchManager.getSearchableInfo(
new ComponentName(getApplicationContext(), SearchActivity.class));
searchView.setSearchableInfo(searchableInfo);
searchView.onActionViewExpanded();
// // 设置搜索文字样式
// EditText searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
// searchEditText.setTextColor(getResources().getColor(R.color.textColorPrimary));
// searchEditText.setHintTextColor(getResources().getColor(R.color.textColorPrimary));
// searchEditText.setBackgroundColor(Color.WHITE);
setOnQuenyTextChangeListener();
return super.onCreateOptionsMenu(menu);
}
private void setupSearchView(MenuItem searchItem) {
//searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
if (searchManager != null) {
List<SearchableInfo> searchables = searchManager.getSearchablesInGlobalSearch();
SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
for (SearchableInfo inf : searchables) {
if (inf.getSuggestAuthority() != null
&& inf.getSuggestAuthority().startsWith("applications")) {
info = inf;
}
}
mSearchView.setSearchableInfo(info);
}
mSearchView.setOnQueryTextListener(this);
}
protected void onFinishSetupOptionsMenu(final Menu menu) {
searchItem = menu.findItem(R.id.menu_global_search);
if(searchItem != null) {
searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
SearchManager searchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo = searchManager.getSearchableInfo(context.getComponentName());
if(searchableInfo == null) {
Log.w(TAG, "Failed to get SearchableInfo");
} else {
searchView.setSearchableInfo(searchableInfo);
}
String currentQuery = getCurrentQuery();
if(currentQuery != null) {
searchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
searchView.setQuery(getCurrentQuery(), false);
}
});
}
}
}
private void setupSearchView(MenuItem searchItem) {
mSearchView.setIconifiedByDefault(false);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
if (searchManager != null) {
List<SearchableInfo> searchables = searchManager.getSearchablesInGlobalSearch();
SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
for (SearchableInfo inf : searchables) {
if (inf.getSuggestAuthority() != null
&& inf.getSuggestAuthority().startsWith("applications")) {
info = inf;
}
}
mSearchView.setSearchableInfo(info);
}
mSearchView.setOnQueryTextListener(this);
mSearchView.setFocusable(false);
mSearchView.setFocusableInTouchMode(false);
}
private void setupSearchView(MenuItem searchItem) {
mSearchView.setIconifiedByDefault(false);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
if (searchManager != null) {
List<SearchableInfo> searchables = searchManager.getSearchablesInGlobalSearch();
SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
for (SearchableInfo inf : searchables) {
if (inf.getSuggestAuthority() != null
&& inf.getSuggestAuthority().startsWith("applications")) {
info = inf;
}
}
mSearchView.setSearchableInfo(info);
}
mSearchView.setOnQueryTextListener(this);
mSearchView.setFocusable(false);
mSearchView.setFocusableInTouchMode(false);
}
/**
* Sets the SearchableInfo for this SearchView. Properties in the SearchableInfo are used
* to display labels, hints, suggestions, create intents for launching search results screens
* and controlling other affordances such as a voice button.
*
* @param searchable a SearchableInfo can be retrieved from the SearchManager, for a specific
* activity or a global search provider.
*/
public void setSearchableInfo(SearchableInfo searchable) {
mSearchable = searchable;
if (mSearchable != null) {
updateSearchAutoComplete();
updateQueryHint();
}
// Cache the voice search capability
mVoiceButtonEnabled = hasVoiceSearch();
if (mVoiceButtonEnabled) {
// Disable the microphone on the keyboard, as a mic is displayed near the text box
// TODO: use imeOptions to disable voice input when the new API will be available
mQueryTextView.setPrivateImeOptions(IME_OPTION_NO_MICROPHONE);
}
updateViewsVisibility(isIconified());
}
private void onVoiceClicked() {
// guard against possible race conditions
if (mSearchable == null) {
return;
}
SearchableInfo searchable = mSearchable;
try {
if (searchable.getVoiceSearchLaunchWebSearch()) {
Intent webSearchIntent = createVoiceWebSearchIntent(mVoiceWebSearchIntent,
searchable);
getContext().startActivity(webSearchIntent);
} else if (searchable.getVoiceSearchLaunchRecognizer()) {
Intent appSearchIntent = createVoiceAppSearchIntent(mVoiceAppSearchIntent,
searchable);
getContext().startActivity(appSearchIntent);
}
} catch (ActivityNotFoundException e) {
// Should not happen, since we check the availability of
// voice search before showing the button. But just in case...
Log.w(LOG_TAG, "Could not find voice search activity");
}
}
/**
* Sets the SearchableInfo for this SearchView. Properties in the SearchableInfo are used
* to display labels, hints, suggestions, create intents for launching search results screens
* and controlling other affordances such as a voice button.
*
* @param searchable a SearchableInfo can be retrieved from the SearchManager, for a specific
* activity or a global search provider.
*/
public void setSearchableInfo(SearchableInfo searchable) {
mSearchable = searchable;
if (mSearchable != null) {
updateSearchAutoComplete();
updateQueryHint();
}
// Cache the voice search capability
mVoiceButtonEnabled = hasVoiceSearch();
if (mVoiceButtonEnabled) {
// Disable the microphone on the keyboard, as a mic is displayed near the text box
// TODO: use imeOptions to disable voice input when the new API will be available
mQueryTextView.setPrivateImeOptions(IME_OPTION_NO_MICROPHONE);
}
updateViewsVisibility(isIconified());
}
private void onVoiceClicked() {
// guard against possible race conditions
if (mSearchable == null) {
return;
}
SearchableInfo searchable = mSearchable;
try {
if (searchable.getVoiceSearchLaunchWebSearch()) {
Intent webSearchIntent = createVoiceWebSearchIntent(mVoiceWebSearchIntent,
searchable);
getContext().startActivity(webSearchIntent);
} else if (searchable.getVoiceSearchLaunchRecognizer()) {
Intent appSearchIntent = createVoiceAppSearchIntent(mVoiceAppSearchIntent,
searchable);
getContext().startActivity(appSearchIntent);
}
} catch (ActivityNotFoundException e) {
// Should not happen, since we check the availability of
// voice search before showing the button. But just in case...
Log.w(LOG_TAG, "Could not find voice search activity");
}
}
private void setupSearchView(MenuItem searchItem) {
if (isAlwaysExpanded()) {
mSearchView.setIconifiedByDefault(false);
} else {
searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
}
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
if (searchManager != null) {
List<SearchableInfo> searchables = searchManager.getSearchablesInGlobalSearch();
// Try to use the "applications" global search provider
SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
for (SearchableInfo inf : searchables) {
if (inf.getSuggestAuthority() != null
&& inf.getSuggestAuthority().startsWith("applications")) {
info = inf;
}
}
mSearchView.setSearchableInfo(info);
}
mSearchView.setOnQueryTextListener(this);
}
/**
* Sets the SearchableInfo for this SearchView. Properties in the SearchableInfo are used
* to display labels, hints, suggestions, create intents for launching search results screens
* and controlling other affordances such as a voice button.
*
* @param searchable a SearchableInfo can be retrieved from the SearchManager, for a specific
* activity or a global search provider.
*/
public void setSearchableInfo(SearchableInfo searchable) {
mSearchable = searchable;
if (mSearchable != null) {
updateSearchAutoComplete();
updateQueryHint();
}
// Cache the voice search capability
mVoiceButtonEnabled = hasVoiceSearch();
if (mVoiceButtonEnabled) {
// Disable the microphone on the keyboard, as a mic is displayed near the text box
// TODO: use imeOptions to disable voice input when the new API will be available
mQueryTextView.setPrivateImeOptions(IME_OPTION_NO_MICROPHONE);
}
updateViewsVisibility(isIconified());
}
private void onVoiceClicked() {
// guard against possible race conditions
if (mSearchable == null) {
return;
}
SearchableInfo searchable = mSearchable;
try {
if (searchable.getVoiceSearchLaunchWebSearch()) {
Intent webSearchIntent = createVoiceWebSearchIntent(mVoiceWebSearchIntent,
searchable);
getContext().startActivity(webSearchIntent);
} else if (searchable.getVoiceSearchLaunchRecognizer()) {
Intent appSearchIntent = createVoiceAppSearchIntent(mVoiceAppSearchIntent,
searchable);
getContext().startActivity(appSearchIntent);
}
} catch (ActivityNotFoundException e) {
// Should not happen, since we check the availability of
// voice search before showing the button. But just in case...
Log.w(LOG_TAG, "Could not find voice search activity");
}
}
/**
* Returns the SearchableInfo for a given activity.
*
* @param launchActivity The activity from which we're launching this search.
* @return Returns a SearchableInfo record describing the parameters of the search,
* or null if no searchable metadata was available.
*/
@Override
public SearchableInfo getSearchableInfo(final ComponentName launchActivity) {
if (launchActivity == null) {
Log.e(TAG, "getSearchableInfo(), activity == null");
return null;
}
return getSearchables(UserHandle.getCallingUserId()).getSearchableInfo(launchActivity);
}