找不到处理意图的活动 { act=android.speech.action.RECOGNIZE_SPEECH (具有额外信息) }

9
异常是在以下代码中抛出的:
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);.

我进行了谷歌搜索,发现问题是由于我使用的设备缺少来自谷歌的语音搜索应用程序。我可以通过手动安装该应用程序来解决问题,但是我该如何以编程方式获取apk安装程序,例如导入某个库或其他方法?
非常感谢。


1
问题与“语音搜索应用程序”的链接目前已损坏(404)。 - Silas S. Brown
2个回答

9
在Web视图中打开您想要使用的应用程序链接。
try{
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);.
}
catch(ActivityNotFoundException e)
{
Intent browserIntent = new Intent(Intent.ACTION_VIEW,   Uri.parse("https://market.android.com/details?id=APP_PACKAGE_NAME"));
startActivity(browserIntent);

}

https://market.android.com/details?id=APP_PACKAGE_NAME中的APP_PACKAGE_NAME替换为市场上语音识别应用的包名称。


感谢您的回复,Vipin。但是,我可以将语音模块作为我的 APK 的一部分添加吗? - Bolton
当然可以,但是你必须自己编写整个代码。 - vipin
@vipin 当您提到APP_PACKAGE_NAME时,您知道是否有默认的包名吗?我的其他设备都可以正常使用,但在Sony Experia Mini上语音识别无法工作。因此,我想将用户指向来自Google的默认语音识别。 - IgorGanapolsky
2
Igor,谷歌语音搜索没有默认设置,但你可以使用谷歌应用程序来满足你的需求,请尝试使用com.google.android.voicesearch。 - vipin
这个包 voicesearch 已经不再可用了。 - Luciano Brum

7

Vipin的解决方案有效。 我个人使用以下内容作为我的APP_PACKAGE_NAME: com.google.android.googlequicksearchbox

因此,为了总结完整的解决方案,您需要执行以下步骤:(我稍微修改了一下,首先尝试market://格式,如果失败则退回到https://格式。)

try {
    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);.
} catch(ActivityNotFoundException e) {
    String appPackageName = "com.google.android.googlequicksearchbox";
    try {
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
    } catch (android.content.ActivityNotFoundException anfe) {
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接