仅获取我的Android应用程序附带的区域设置列表

仅获取我的Android应用程序附带的区域设置列表,android,locale,Android,Locale,我知道有几种方法可以获得设备支持的所有区域设置的列表。是否有人能够获取您在应用程序中包含的区域设置列表 使用以下代码,我知道我可以获得设备支持的列表: String[] languages = getAssets().getLocales(); 或 但是我想要我的应用程序附带的列表,这样我就可以将它与手机支持的列表进行比较,根据手机支持和应用程序支持创建子集列表 显然,我可以提供一个包含我支持的语言列表的文件,但这不是我想要采用的方法。public ArrayList getTranslat

我知道有几种方法可以获得设备支持的所有区域设置的列表。是否有人能够获取您在应用程序中包含的区域设置列表

使用以下代码,我知道我可以获得设备支持的列表:

String[] languages =  getAssets().getLocales();

但是我想要我的应用程序附带的列表,这样我就可以将它与手机支持的列表进行比较,根据手机支持和应用程序支持创建子集列表

显然,我可以提供一个包含我支持的语言列表的文件,但这不是我想要采用的方法。

public ArrayList getTranslatedLocales(int-compairsonStringResourceID,boolean only是电话支持的){
public ArrayList<Locale> getTranslatedLocales(int compairsonStringResourceID, boolean onlyThoseSupportedByThePhone) {

Locale english = Locale.ENGLISH;

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Resources resources = getResources();
Configuration configuration = resources.getConfiguration();
Locale assignedLanguage = configuration.locale;
ArrayList<Locale> loc = new ArrayList<Locale>();  
ArrayList<String> processed = new ArrayList<String>();
String englishConst = english.getLanguage();
if(onlyThoseSupportedByThePhone) {
    String[] locales =  resources.getAssets().getLocales();
    for (String string : locales) {
        if(!string.startsWith(englishConst) && !processed.contains(string)) {
            processed.add(string);
            loc.add(new Locale(string));
        }
    }
} else {
    Locale[] locales = Locale.getAvailableLocales();
    for (Locale locale : locales) {
        String language = locale.getLanguage();
        if(!locale.getLanguage().equals(englishConst) && !processed.contains(language)) {
            processed.add(language);
            loc.add(locale);
        }
    }
}

configuration.locale = english;
Resources res = new Resources(getAssets(), metrics, configuration);
String compareString = res.getString(compairsonStringResourceID);

ArrayList<Locale> supported = new ArrayList<Locale>();
supported.add(english);

for (int i = 0; i < loc.size(); i++) {
    configuration.locale = loc.get(i);
    res = new Resources(getAssets(), metrics, configuration);
    Resources res2 = new Resources(getAssets(), metrics, configuration);
    String s2 = res2.getString(compairsonStringResourceID);

    if(!compareString.equals(s2)){
        supported.add(configuration.locale);
    }
}

configuration.locale = assignedLanguage;
setLocale(assignedLanguage);
return supported;
Locale english=Locale.english; DisplayMetrics=新的DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(度量); Resources=getResources(); 配置=resources.getConfiguration(); Locale assignedLanguage=configuration.Locale; ArrayList loc=新的ArrayList(); ArrayList processed=新的ArrayList(); 字符串englishConst=english.getLanguage(); 如果(仅适用于手机支持的功能){ String[]locales=resources.getAssets().getLocales(); for(字符串:区域设置){ 如果(!string.startsWith(englishConst)&&!processed.contains(string)){ 已处理。添加(字符串); loc.add(新语言环境(字符串)); } } }否则{ Locale[]locales=Locale.getAvailableLocales(); for(区域设置:区域设置){ String language=locale.getLanguage(); 如果(!locale.getLanguage().equals(englishConst)&&!processed.contains(language)){ 已处理。添加(语言); 位置添加(区域设置); } } } configuration.locale=英语; Resources res=新资源(getAssets(),度量,配置); 字符串比较字符串=res.getString(compairsonStringResourceID); 支持的ArrayList=新的ArrayList(); 支持。添加(英语); 对于(int i=0;i

}

请参见:谢谢,这很有效。将发布我修改的solution@d3n13d1您能否将您的
解决方案
移动到一个答案并接受您自己的答案。所以其他人实际上看到了答案。
public ArrayList<Locale> getTranslatedLocales(int compairsonStringResourceID, boolean onlyThoseSupportedByThePhone) {

Locale english = Locale.ENGLISH;

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Resources resources = getResources();
Configuration configuration = resources.getConfiguration();
Locale assignedLanguage = configuration.locale;
ArrayList<Locale> loc = new ArrayList<Locale>();  
ArrayList<String> processed = new ArrayList<String>();
String englishConst = english.getLanguage();
if(onlyThoseSupportedByThePhone) {
    String[] locales =  resources.getAssets().getLocales();
    for (String string : locales) {
        if(!string.startsWith(englishConst) && !processed.contains(string)) {
            processed.add(string);
            loc.add(new Locale(string));
        }
    }
} else {
    Locale[] locales = Locale.getAvailableLocales();
    for (Locale locale : locales) {
        String language = locale.getLanguage();
        if(!locale.getLanguage().equals(englishConst) && !processed.contains(language)) {
            processed.add(language);
            loc.add(locale);
        }
    }
}

configuration.locale = english;
Resources res = new Resources(getAssets(), metrics, configuration);
String compareString = res.getString(compairsonStringResourceID);

ArrayList<Locale> supported = new ArrayList<Locale>();
supported.add(english);

for (int i = 0; i < loc.size(); i++) {
    configuration.locale = loc.get(i);
    res = new Resources(getAssets(), metrics, configuration);
    Resources res2 = new Resources(getAssets(), metrics, configuration);
    String s2 = res2.getString(compairsonStringResourceID);

    if(!compareString.equals(s2)){
        supported.add(configuration.locale);
    }
}

configuration.locale = assignedLanguage;
setLocale(assignedLanguage);
return supported;