0

我用英語製作應用程序。我的應用使用語音識別。但是,如果我使用另一種系統語言(例如法語或俄語)在設備上安裝此應用程序。我的語音識別不起作用。它僅適用於系統默認的語言。我的應用程序默認情況下如何爲語音識別製作英語語言?在我的應用程序中更改語音識別的默認語言

我發現這個方法,但它不工作

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()); 
+0

檢查出了完整的代碼要設置爲Android語音識別的語言? –

+0

是的,默認情況下它只適用於我的應用程序。可能嗎? – Nikolai

回答

3

您可以使用此代碼嘗試:

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

+0

謝謝!我只是改變意圖從我的方式從Locale.ENGLISH到「en-US」,它開始工作!感謝您的鏈接! – Nikolai