ActivityNotFoundException: 未找到 Activity 来处理 Intent (RECOGNIZE_SPEECH)

ActivityNotFoundException: No Activity found to handle Intent (RECOGNIZE_SPEECH)

我正在尝试使用语音识别器制作一个应用程序。这是我的一段代码:

public class Habla extends Activity{

   private static int code = 123;
   ...
   public void escuchar()
   {
       Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
       intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, languageModel);

       intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, numberResults);
       startActivityForResult(intent, code);
    }
...
}

错误如下:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.speech.action.RECOGNIZE_SPEECH (has extras) }

为什么会这样? .Habla 是一个 class,它是 运行 在 .MainActivity 中按下一个按钮,所以 AndoridManifest 是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.emiliomorillanieto.practica3" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".Habla"
            android:parentActivityName=".MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
   </application>
</manifest>

您应该先检查是否安装了识别应用程序:

PackageManager manager = context.getPackageManager();
List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0);
    if (infos.size() > 0) {
         //Then there is application can handle your intent
    }else{
         //No Application can handle your intent
    }

原因是您使用的设备缺少 google 中的 voice search app。您可以通过在设备上手动安装来解决问题。但是还有另一种方法可以做到这一点。这就是在网络视图中打开应用程序的 link,如下所示

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 your_browser_intent = new Intent(Intent.ACTION_VIEW,         

    Uri.parse("https://market.android.com/details?id=APP_PACKAGE_NAME"));
    startActivity(your_browser_intent);
}

您也可以通过编码而不是使用 webview 来完成,但这需要大量工作并且您需要编写一大堆代码。所以,我认为使用 webview 非常好。

如果您的设备带有自定义 ROM:

下载:Google App.apk [https://www.apkmirror.com/apk/google-inc/google-search/] 或 .app,如果您有权访问 Google Play Store。

使用 adb 命令安装 "adb install path_to_apk" [https://developer.android.com/studio/command-line/adb.html?hl=en#move])

如果不行,请同时下载:Google播放Services.apk

我知道这个问题已经超过 1 年了,但我在使用机器人(使用 Android OS 4.4.4 API 19 和自定义 ROM)和使用 RecognizerIntent 时遇到了问题对我帮助很大