如何从具有单实例启动模式的活动正确启动语音识别活动?

发布于 2024-12-22 17:03:15 字数 544 浏览 0 评论 0原文

已经看到另一个线程,其中提到如果从单实例启动模式的活动。所以我想知道我的替代方案是什么。

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

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

Already saw another thread which mentions that an activity with RecognizerIntent was not working correctly if launched from within an activity with singleInstance launch mode. So I would like to know what my alternatives are.

My use case is as follows: My application listens for an event, and when this event occurs, it displays an alert dialog, even if the user is in the midst of using another application. From other questions I found that the common way of doing this is to launch an activity with singleInstance launch mode. But now once this alert dialog pops up, I need to use RecognizerIntent and do some speech to text processing. However speech input dialog just does not wait for any input and onActivityResult() is called immediately. Things work fine if my alert dialog pops up from an activity which has a launch mode other than "singleInstance".

Are there other ways to approach this problem ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

强者自强 2024-12-29 17:03:15

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

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() 内部,而下面的代码应该写在 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 上测试了它,一旦您在列表中查看文本列表,它就可以很好地工作由您选择。

Try running your Code this way :-

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");
   }

Above code should be written inside onCreate() while below should be written ouside it

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 
}

I have tested it on DellXCD35 android 2.3.3 and it works perfectly well once you get list of texts in your list view it upto you whihch you want to select.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文