【问题标题】:onActivityResult never called for speech Intent on Google GlassonActivityResult 从未在 Google Glass 上调用语音 Intent
【发布时间】:2014-01-24 16:01:37
【问题描述】:

我有一个带有多个选项的菜单 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 上设置结果(),因为它是 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);
  }

【问题讨论】:

    标签: android android-intent android-activity google-glass google-gdk


    【解决方案1】:

    你说这是一个“菜单活动”,那么这是否意味着它附加在一张实时卡片上?

    如果是这样,您是否会覆盖 onOptionsMenuClosed 并在其中调用 finish

    如果是,菜单活动将在语音活动返回之前完成并自行销毁,因此结果将无处可返回。

    解决此问题的一种方法是使用一个标志来指示您是否应该在菜单关闭时将调用推迟到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();
        }
    }
    

    【讨论】:

    • 你认为你可以举一个例子来说明如何做到这一点吗?我不完全确定该怎么做。
    猜你喜欢
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 2018-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多