我有一个带有多个选项的菜单 Activity 。其中之一是使用 Glass 的语音转文本功能发布到新闻源。我看了https://developers.google.com/glass/develop/gdk/input/voice#starting_the_speech_recognition_activity 并全部实现,但 onActivityResult 方法显然永远不会被调用。
在 Glass 设备上,我可以在菜单中选择“新帖子”,然后就会出现语音捕捉功能。我可以对它说话,它会将我的语音转换为屏幕上的文本。但在我接受它后(通过点击或等待几秒钟),它就会退出并带我回到主屏幕。我需要能够在 onActivityResult 中获取语音文本字符串,并调用另一个方法 (displayPostMenu) 来显示另一个菜单来处理文本,但如果从未调用 onActivityResult,我就无法做到这一点。
我已经研究了几个类似的问题,但没有一个解决方案有效/适用...我不认为我可以在 RecognizerIntent.ACTION_RECOGNIZE_SPEECH 上 setResult() 因为它是 Google 的?任何帮助将不胜感激!
我的一些代码:
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);
}
最佳答案
你说这是一个“菜单 Activity ”,那么这是否意味着它附加到实时卡上?
如果是这样,您是否重写了 onOptionsMenuClosed
并在其中调用 finish
?
如果是,菜单 Activity 将在语音 Activity 返回之前完成并自行销毁,因此结果将没有地方可返回。
解决此问题的一种方法是使用一个标志来指示是否应该在菜单关闭时推迟对 finish
的调用,并在 onOptionsMenuClosed
内有条件地进行该调用> 基于该标志。然后,在 displaySpeechRecognizer
方法中设置该标志,并等到 onActivityResult
处理结束时调用 finish
。
这样的东西应该可以工作(未经测试编写,可能包含拼写错误):
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();
}
}
关于android - onActivityResult 从未在 Google Glass 上调用语音 Intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21337093/