Reputation: 23
I have a menu Activity with several options. One of them is to post to a news feed using Glass's speech-to-text. I looked at https://developers.google.com/glass/develop/gdk/input/voice#starting_the_speech_recognition_activity and have it all implemented, but the onActivityResult method apparently never gets called.
On the Glass device, I'm able to select "New Post" in the menu and the voice capture comes up. I can speak to it and it'll convert my speech to text on the screen. But after I accept it (by tapping or waiting a few seconds), it just exits and takes me back to the home screen. I need to be able to get the speech text String in onActivityResult and call another method (displayPostMenu) to show another menu to process the text but I can't do that if onActivityResult is never called.
I've looked at several similar issues but none of the solutions have worked/were applicable... I don't think I can setResult() on RecognizerIntent.ACTION_RECOGNIZE_SPEECH since it's Google's? Any help would be greatly appreciated!
Some pieces of my code:
private final int SPEECH_REQUEST = 1;
//Code to make this Activity work and the menu open...
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.view_feed:
//Stuff
return true;
case R.id.new_post:
Log.i("MainMenu", "Selected new_post");
displaySpeechRecognizer();
Log.i("MainMenu", "Ran displaySpeechRecog under new_post selection");
return true;
case R.id.stop:
Activity parent = getParent();
Log.i("MainMenu", "Closing activity; parent: " + parent + "; " + hashCode());
if (parent != null && parent.getApplication() == getApplication()) {
finish();
} else {
MainMenu.close();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onOptionsMenuClosed(Menu menu) {
// Nothing else to do, closing the Activity.
finish();
}
public void displaySpeechRecognizer() {
Log.i("MainMenu", "Entered displaySpeechRecognizer");
Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
startActivityForResult(speechIntent, SPEECH_REQUEST);
Log.i("MainMenu", "Finished displaySpeechRecognizer. startActivityForResult called.");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("MainMenu", "onActivityResult entered from MainMenu");
switch (requestCode) {
case SPEECH_REQUEST:
Log.i("MainMenu", "onActivityResult enters SPEECH_REQUEST case");
if (resultCode == RESULT_OK) {
Log.i("MainMenu", "onActivityResult enters RESULT_OK for voice cap");
List<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String spokenText = results.get(0);
Log.i("MainMenu", "SpokenText:" + spokenText);
holdText = spokenText;
if (holdText != "") {
displayPostMenu();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
Upvotes: 2
Views: 1206
Reputation: 6429
You say this is a "menu activity", so does that mean that it's attached to a live card?
If so, are you overriding onOptionsMenuClosed
and calling finish
inside it?
If you are, the menu activity will finish and destroy itself before the speech activity returns, so there will be no place for the result to go back to.
One approach to fix this would be to use a flag to indicate whether you should defer the call to finish
when the menu is closed, and make that call conditionally inside onOptionsMenuClosed
based on that flag. Then, set that flag in your displaySpeechRecognizer
method and wait until the end of your onActivityResult
processing to call finish
instead.
Something like this should work (written without testing, may contain typos):
private boolean shouldFinishOnMenuClose;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// By default, finish the activity when the menu is closed.
shouldFinishOnMenuClose = true;
// ... the rest of your code
}
private void displaySpeechRecognizer() {
// Clear the flag so that the activity isn't finished when the menu is
// closed because it will close when the speech recognizer appears and
// there won't be an activity to send the result back to.
shouldFinishOnMenuClose = false;
// ... the rest of your code
}
@Override
public void onOptionsMenuClosed(Menu menu) {
super.onOptionsMenuClosed();
if (shouldFinishOnMenuClose) {
finish();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SPEECH_REQUEST) {
if (resultCode == RESULT_OK) {
// process the speech
}
// *Now* it's safe to finish the activity. Note that we do this
// whether the resultCode is OK or something else (so the menu
// activity goes away even if the user swipes down to cancel
// the speech recognizer).
finish();
}
}
Upvotes: 4