问题
I'm trying to inject the transcribed text in the search query in order to filter my adapter list, but, after trying every combination I could imagine, I come here to ask for help.
For instance, if handle the intent after a new intent and set text to a TextView
everything works fine and the voice recorder transcribes what is said correctly, but I'm not being able to get the same String
and set the SearchView
query successfully.
The keyboard microphone, otherwise, captures the text and pass it to the query perfectly; the same procedure isn't being possible while using the SearchView
voice search after clicking the icon though.
I aware of the following:
Note: Carefully consider whether voice search is appropriate for your application. All searches performed with the voice search button are immediately sent to your searchable activity without a chance for the user to review the transcribed query. Sufficiently test the voice recognition and ensure that it understands the types of queries that the user might submit inside your application.
However, it seems some developers have been able to do this and my main issue seems to be related with the SearchView
which isn't initialized before the intent tries to set a new query on it since it's done on the scope of the onCreateOptionsMenu()
rather than onCreate()
.
Here is the code I'm using:
- the
searchable
configuration:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/search_hint"
android:label="@string/app_name"
android:voiceLanguageModel="web_search"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer">
</searchable>
- the
SearchView
inside theMenu
:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) searchItem.getActionView();
assert searchManager != null;
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
}
);
> Blockquote
searchView.getQuery();
return true;
}
onNewIntent()
:
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
- and the way I was trying to inject the
String
in theSearchView
fromsetQuery()
with no lucky:
if (Intent.ACTION_SEARCH.equals(getIntent().getAction())) {
String searchQuery = getIntent().getStringExtra(SearchManager.QUERY);
searchView.setQuery(searchQuery, true);
adapter.getFilter().filter(searchQuery);
}
Thanks in advance!
来源:https://stackoverflow.com/questions/57555633/how-to-setquery-from-voice-search