问题
I make an app in English. My app uses Speech recognition. But if I install this app on device with another system language, French or Russian for example. My speech recognition doesn't work. It works only for language which by default in system. How can I make English language for Speech recognition by default for my app?
I found this method but it doesn't work
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());
回答1:
You can try with this code:
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
Also, your app can query for the list of supported languages by sending a RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS ordered broadcast like so:
Intent detailsIntent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
sendOrderedBroadcast(
detailsIntent, null, new LanguageDetailsChecker(), null, Activity.RESULT_OK, null, null);
where LanguageDetailsChecker is something like this:
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);
}
}
}
You can also check out the complete code for that at here:https://github.com/gast-lib
来源:https://stackoverflow.com/questions/33029417/change-default-language-for-speech-recognition-in-my-app