如何更改android应用的语言?不建议使用语言环境

问题描述 投票:0回答:2

我刚刚完成我的应用程序,但是在更改应用程序语言的方法上遇到此错误

Setting.java:uses or overrides a deprecated API.Recompile with -Xlint:deprecation for details.

这是我的方法

private void setLanguage(String language){

    //setting new configuration
    Locale locale = new Locale(language);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    con.getApplicationContext().getResources().updateConfiguration(config, null);

    //store current language in prefrence
    prefData.setCurrentLanguage(language);

    //With new configuration start activity again
    Intent intent = new Intent(getApplicationContext(),MainActivity.class);
    startActivity(intent);
    finish();

}

我如何克服这个问题并发布我的应用。在此先感谢

java android locale
2个回答
1
投票
config.locale = locale;

从Android.N开始不推荐使用,因此请使用以下代码:

    @SuppressWarnings("deprecation")
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        config.setLocale(locale);
    } else {
        config.locale = locale;
    }

加上重新启动活动的最佳方法是致电:

recreate();

0
投票

我有同样的问题。这对我有用。

    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration  conf = res.getConfiguration();
    conf.setLocale(new Locale("es"));
    res.updateConfiguration(conf, dm);

0
投票

我有同样的问题。这对我有用。

    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration  conf = res.getConfiguration();
    conf.setLocale(new Locale("es"));
    res.updateConfiguration(conf, dm);
© www.soinside.com 2019 - 2024. All rights reserved.