【发布时间】:2018-09-28 02:49:25
【问题描述】:
在大多数 Android 设备中,RecognitionService 将由 Google 的原生“Now/Assistant”应用程序提供。
在 Android Oreo 之前,我可以使用以下简单代码查询 Google 识别器支持的语言:
final Intent vrIntent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
// vrIntent.setPackage("com.google.android.googlequicksearchbox");
getContext().sendOrderedBroadcast(vrIntent, null, new BroadcastReceiver() {
@Override
public void onReceive(final Context context, final Intent intent) {
// final Bundle bundle = intent.getExtras();
final Bundle bundle = getResultExtras(true);
if (bundle != null) {
if (bundle.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES)) {
Log.i("TAG", "onReceive: EXTRA_SUPPORTED_LANGUAGES present");
final ArrayList<String> vrStringLocales = bundle.getStringArrayList(
RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);
Log.i("TAG", "onReceive: EXTRA_SUPPORTED_LANGUAGES size: " + vrStringLocales.size());
} else {
Log.w("TAG", "onReceive: missing EXTRA_SUPPORTED_LANGUAGES");
}
} else {
Log.w("TAG", "onReceive: Bundle null");
}
}, null, 1234, null, null);
但是,从 8.0+ 开始,额外的 RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES 不再包含在响应中。
在我尝试将此作为错误提交之前,我想先看看其他人是否可以复制 - 但还要检查是否有一个有序广播 behavioural change in API 26 我不知何故忽略了,这可能是造成这种情况的原因。
提前致谢。
【问题讨论】:
-
您是否尝试过使用
getVoiceDetailsIntent()? -
@azizbekian 谢谢。我有 - 它只是返回
RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS的显示意图 -
能否通过检查
getResultCode() == Activity.RESULT_OK或getResultData()在receive()函数开头是否包含任何值来做简单的调试和验证Broadcast是否成功完成? -
@R.Zagórski 结果代码与请求代码匹配,在上面的示例中为 1234。结果数据为空,但文档确实声明“这通常为空”。此行为与 pre-Oreo 相同,后者确实包含所需的额外语言。
-
我已经用 API 26 在 Android Emulator 上测试了你的代码,我到达了这个
Log.i("TAG", "onReceive: EXTRA_SUPPORTED_LANGUAGES present");。compileSdkVersion 27 buildToolsVersion "27.0.3" defaultConfig { applicationId "it.versionestabile.stackover001" minSdkVersion 19 targetSdkVersion 27 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" }你是说没有达到那个代码吗?
标签: java android speech-recognition voice-recognition google-voice-search