java - Android context.getResources.updateConfiguration() deprecated 如何解决?

标签 java android locale deprecated

就在最近,context.getResources().updateConfiguration() 已在 Android API 25 中弃用,我需要更改用户选择的应用语言。我正在使用这种方法来更改语言

  private void setLanguage(String language){
    Locale locale = new Locale(language);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    con.getApplicationContext().getResources().updateConfiguration(config, null);
    prefData.setCurrentLanguage(language);
    Intent intent = new Intent(getApplicationContext(),MainActivity.class);
    startActivity(intent);
    finish();

}

但是得到弃用的 api 警告,我刚刚开始 android 开发。因此任何建议都将有助于解决此警告。

最佳答案

我在 Resource 源代码中找到了以下代码。

 /**
     * Store the newly updated configuration.
     *
     * @deprecated See {@link android.content.Context#createConfigurationContext(Configuration)}.
     */
    @Deprecated
    public void updateConfiguration(Configuration config, DisplayMetrics metrics) {
        updateConfiguration(config, metrics, null);
    }

文档说您应该使用 Context.createConfigurationContext(Configuration) 代替它已被弃用。

关于java - Android context.getResources.updateConfiguration() deprecated 如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54774485/

相关文章:

java - 将 Java 与模块一起使用

Java trunc() 等效方法

java - 为什么 Android 中的 Locale.getDefault().getLanguage() 返回显示名称而不是语言代码?

java - Android 区域设置中奇怪的语言代码

根据区域设置国家信息检索时区(与操作系统无关)

java - 使用 aether-api 列出 sonatype Nexus 的可用版本

java - Java如何高效管理内存

android - 尝试将文件从 SD 卡附加到电子邮件

android - 在 listview 上过滤自定义适配器在 android 中无法正常工作?

java - 如何通过 DialogFragment 使用 startActivityForResult()?