【问题标题】:How to correctly launch Voice Recognition activity from an activity with singleInstance launch mode?如何从具有单实例启动模式的活动中正确启动语音识别活动?
【发布时间】:2011-12-23 00:17:27
【问题描述】:

已经看到另一个thread,其中提到如果从具有单实例启动模式的活动中启动,具有识别器意图的活动将无法正常工作。所以我想知道我的替代方案是什么。

我的用例如下:我的应用程序监听一个事件,当这个事件发生时,它会显示一个警告对话框,即使用户正在使用另一个应用程序。从其他questions 我发现这样做的常见方法是使用单实例启动模式启动一个活动。但是现在一旦弹出这个警告对话框,我需要使用 RecognizerIntent 并对文本进行一些语音处理。然而,语音输入对话框不会等待任何输入,并且会立即调用 onActivityResult()。如果我的警报对话框从具有“singleInstance”以外的启动模式的活动中弹出,则一切正常。

还有其他方法可以解决这个问题吗?

【问题讨论】:

  • 这个solution 帮助我解决了上述问题。

标签: android android-alertdialog launchmode recognizer-intent


【解决方案1】:

尝试以这种方式运行您的代码:-

List<ResolveInfo> activities = pm.queryIntentActivities(
            new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
    if (activities.size() != 0) {
        speakButton.setOnClickListener(this);
    } else {
        speakButton.setEnabled(false);
        speakButton.setText("Recognizer not present");
   }

上面的代码应该写在 onCreate() 里面,下面的代码应该写在外面

public void onClick(View v) {
    if (v.getId() == R.id.btn_speak) {
        startVoiceRecognitionActivity();
    }
}


 private void startVoiceRecognitionActivity() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
    startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);

//Run a loop checking whether the list is empty or not:-
    while(activities.isEmpty()){
        //wait    
    }
//Now run your alert dialog box 
}

我已经在 DellXCD35 android 2.3.3 上对其进行了测试,一旦您在列表中获得了文本列表,它就可以很好地工作,您可以自行选择要选择的文本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多