在我的应用程序中更改语音识别的默认语言

Change default language for Speech recognition in my app

我用英语制作了一个应用程序。我的应用程序使用语音识别。但是,如果我在使用其他系统语言(例如法语或俄语)的设备上安装此应用程序。我的语音识别不起作用。它仅适用于系统默认的语言。如何为我的应用程序默认使用英语进行语音识别?

我找到了这个方法,但是没有用

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

查看完整代码