Android 在我设置Locale.setDefault(Locale)之后,我怎样才能找回手机语言?
我使用此代码段允许用户在应用程序中设置他最喜欢的语言环境:Android 在我设置Locale.setDefault(Locale)之后,我怎样才能找回手机语言?,android,locale,Android,Locale,我使用此代码段允许用户在应用程序中设置他最喜欢的语言环境: Locale locale = new Locale(newLan); Locale.setDefault(locale); Configuration config = getBaseContext().getResources().getConfiguration(); config.locale = locale; getBaseContext().getResources().updateConfiguration(c
Locale locale = new Locale(newLan);
Locale.setDefault(locale);
Configuration config = getBaseContext().getResources().getConfiguration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
问题是,我想写一个设置,允许用户返回到手机的默认语言
怎么可能呢
因为在使用了上面的代码片段并想象用户选择了法语之后,我无法恢复电话的语言环境(例如可能是英语)怎么样:
首先,这段代码
config.locale=locale
已弃用,应使用config.setLocale(locale)代码>
您是否尝试使用locale.getDefault().getDisplayLanguage()获取当前设备区域设置
,并在用户选择默认语言环境作为应用程序的所选语言以及您的代码段后设置它 我刚试过这个,我的手机区域设置是US,toast是用法语显示的,但在日志中我仍然看到我们,也许如果你不将新的区域设置设置为默认值,它仍然可以工作
Locale locale = new Locale("fr");
//Locale.setDefault(locale);
Configuration config = getBaseContext().getResources().getConfiguration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
Toast.makeText(this, android.R.string.cancel, Toast.LENGTH_LONG).show();
Log.d("LOCALE", Locale.getDefault().getCountry());
我在其他问答中看到过使用Locale.setDefault()
,现在我想知道,为什么需要手动设置默认的Locale
?如果这是必要的,那么它不是在updateConfiguration()
中完成的吗?同样有趣的是在应用程序更改语言环境之前,我通过在静态变量中记住设备语言,解决了“返回设备语言”的问题:
public class Global {
public static final Locale systemLocale = Locale.getDefault();
}
您不会更改手机的区域设置,只会在运行时更改应用程序的区域设置。换句话说:当应用程序关闭并再次运行时,区域设置将是默认的上一个区域设置(不是您设置的区域设置)。是否有有效的理由进行否决投票?正如我在第一句中所说,我知道这是应用程序区域设置。。我从来没有说过这是电话现场。我没有投反对票。mate周围到处都是仇恨者。我可以想象,在这个过程中,用户会改变手机的语言环境。。如果我没有得到更好的答案,我会接受你的!因此,在这种情况下,假设您的应用程序正在工作,您可以覆盖活动中的onConfigurationChanged
。如果应用程序关闭,请注册启动的广播,然后检查当前区域设置。(重新启动后)。或者您可以使用操作\u区域设置\u更改
广播。