0

我创建了一个使用语音识别的应用程序,它适用于大多数手机,但是在新的 Galaxy S IV 和 Galaxy Note II 上它失败了:

java.lang.RuntimeException: Unable to start activity ComponentInfo{<my.pakage.myactivity>/<my.pakage.myactivity>}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.speech.action.RECOGNIZE_SPEECH (has extras) }
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
at android.app.ActivityThread.access$700(ActivityThread.java:152)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.speech.action.RECOGNIZE_SPEECH (has extras) }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1659)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1434)
at android.app.Activity.startActivityForResult(Activity.java:3430)
at android.support.v4.app._HoloActivity.superStartActivity(_HoloActivity.java:717)
at android.support.v4.app._HoloActivity.startActivityForResult(_HoloActivity.java:698)
at android.support.v4.app._HoloActivity.startActivityForResult(_HoloActivity.java:689)
at com.ltandfumbles.soundoff.activites.Record.speak(Record.java:263)
at com.ltandfumbles.soundoff.activites.Record.onCreate(Record.java:96)
at android.app.Activity.performCreate(Activity.java:5250)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
... 11 more   

触发这个的代码是:

 void speak() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

        //intent.putExtra(RecognizerIntent.EXTRA_PROMPT, metTextHint.getText().toString());

        // Given an hint to the recognizer about what the user is going to say
        //There are two form of language model available
        //1.LANGUAGE_MODEL_WEB_SEARCH : For short phrases
        //2.LANGUAGE_MODEL_FREE_FORM : If not sure about the words or phrases and its domain.
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);

        int noOfMatches = 3;
        // Specify how many results you want to receive. The results will be
        // sorted where the first result is the one with higher confidence.
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, noOfMatches);
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak now");
        intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 2000);
        intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS, 2000);

        //Start the Voice recognizer activity for the result.
        startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
    }

我意识到错误是由于设备上没有合适的应用程序造成的,但我很难相信新设备没有任何语音识别功能。android 4.2.2 中是否有一些我需要考虑的变化?

4

2 回答 2

0

请确保您已安装/启用 Google 语音搜索。

于 2014-04-24T21:20:17.770 回答
0

处理异常。

无法保证 Intent 会得到匹配,并且随着用户添加和删除应用程序,情况可能会发生变化。可以想象,匹配可能会在未来的 Android 版本中消失,或者在具有与手机完全不同的硬件的 Android 端口中消失。因此,即使它适用于许多 Android 配置,这仍然是您应该处理的异常。

您可以在调用之前检查该操作:

    // Check to see if a recognition activity is present
    PackageManager pm = context.getPackageManager();
    List<ResolveInfo> activities = pm.queryIntentActivities(
            new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),0);
    if (activities.size() == 0) {
          // At this point there is no recognition library and you
          // should handle it here

    }
于 2014-04-24T21:26:25.003 回答