【发布时间】:2020-12-07 05:32:55
【问题描述】:
我一直在使用以下代码来更改 Android 应用中的区域设置(该应用有自己的区域设置,可能与操作系统区域设置不同)。该代码在 Android 9 (P) 之前运行良好。在 Android 10 (Q) 中,它停止工作,资源没有更新。我在 Android 10 发行说明中看不到任何与语言环境相关的更改。什么可以在 Android 10 中破坏此代码?如果它是已知的,谁能指出我的解决方案?
private fun setLocale(context: Context, language: String): Context {
//...persist here. persisting works fine
return if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N)
updateResources(context, language)
else
updateResourcesLegacy(context, language)
}
@TargetApi(Build.VERSION_CODES.N)
private fun updateResources(context: Context, language: String): Context {
val locale = Locale(language)
Locale.setDefault(locale)
val configuration = context.resources.configuration
configuration.setLocale(locale)
configuration.setLayoutDirection(locale)
return context.createConfigurationContext(configuration)
}
UPD:
我发现此代码在升级到androidx.appcompat:appcompat 的较新版本后停止工作。我可以缩小范围:它适用于1.2.0-alpha01,不适用于1.2.0-alpha02。
我在 1.2.0-alpha02 的发行说明中看到与上下文相关的 3 处更改:https://developer.android.com/jetpack/androidx/releases/appcompat#1.2.0-alpha02
- 确保基本上下文始终是包装器 (aosp/1194355)
- 添加了一些改进,以便在修改基本上下文配置时更加智能 (aosp/1204543)
- 为 Robolectric 禁用 createConfigurationContext() (aosp/1186218)
【问题讨论】:
-
如何获取资源?是否有一些资源不会改变或全部?
-
ADM,没有更新。
标签: android android-architecture-components android-jetpack android-10.0