ActivityNotFoundException:找不到处理Intent的活动(RECOGNIZE_SPEECH)

时间:2016-02-09 10:42:02

标签: android android-intent speech-recognition

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

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是一个按下.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>

3 个答案:

答案 0 :(得分:7)

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

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
    }

答案 1 :(得分:4)

原因是您正在使用的设备上缺少谷歌的voice search app。您可以通过在设备上手动安装来解决问题。但还有另一种方法可以做到这一点。这是在webview中打开应用程序的链接,如下面的

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非常好。

答案 2 :(得分:0)

如果您的设备包含自定义ROM:

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

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

如果无效,请同时下载:Google Play Services.apk

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

相关问题