gpt4 book ai didi

android - onActivityResult 从未在 Google Glass 上调用语音 Intent

转载 作者:搜寻专家 更新时间:2023-11-01 08:00:57 24 4
gpt4 key购买 nike

我有一个包含多个选项的菜单 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 上设置结果(),因为它是谷歌的?任何帮助将不胜感激!

我的一些代码:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com