我创建了一个使用语音识别的应用程序,它可以在大多数手机上运行,但是在新的星系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中的一些变化吗?
答案 0 :(得分:0)
请确保您已安装/启用Google语音搜索。
答案 1 :(得分: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
}