我用英语制作了一个应用程序。我的应用程序使用语音识别。但是,如果我在使用其他系统语言(例如法语或俄语)的设备上安装此应用程序。我的语音识别不起作用。它仅适用于系统默认的语言。如何让我的应用程序默认使用英语进行语音识别?
我找到了这个方法,但是不起作用
Locale myLocale;
myLocale = new Locale("English (US)", "en_US");
Locale.setDefault(myLocale);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = myLocale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
最佳答案
您可以尝试使用以下代码:
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
此外,您的应用还可以通过发送 RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS 有序广播来查询支持的语言列表,如下所示:
Intent detailsIntent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
sendOrderedBroadcast(
detailsIntent, null, new LanguageDetailsChecker(), null, Activity.RESULT_OK, null, null);
其中 LanguageDetailsChecker 是这样的:
public class LanguageDetailsChecker extends BroadcastReceiver
{
private List<String> supportedLanguages;
private String languagePreference;
@Override
public void onReceive(Context context, Intent intent)
{
Bundle results = getResultExtras(true);
if (results.containsKey(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE))
{
languagePreference =
results.getString(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE);
}
if (results.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES))
{
supportedLanguages =
results.getStringArrayList(
RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);
}
}
}
您还可以在here:https://github.com/gast-lib查看完整的代码。
关于android - 更改我的应用程序中语音识别的默认语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33029417/