8

在以下代码中引发了异常:

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,比如导入一些库或其他~
非常感谢。

4

2 回答 2

8

在 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替换为市场上的语音识别应用程序包名称

于 2012-02-28T11:39:21.190 回答
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)));
    }
}
于 2016-07-08T20:35:57.380 回答