В настоящее время я установил польский, а английский - система дефолта. build.gradle.kts :
Код: Выделить всё
android {
androidResources {
generateLocaleConfig = true
}
...
}
class MyForegroundServiceStarterWithInvisibleThemeActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// start foreground service - MyForegroundService
finish()
}
}
< /code>
Это нужно «Появиться на вершине», разрешение (или другие), конечно, иметь возможность начать действия с фона. < /p>
val Context.languageContext: Context
get() = ContextCompat.getContextForLanguage(this)
< /code>
Сервис, в которой мне нужно получить локализованные строки (для уведомлений, тостов и т. Д.): < /p>
@AndroidEntryPoint
class MyForegroundService : LifecycleService() {
private lateinit var context: Context
override fun onCreate() {
super.onCreate()
context = languageContext
Log.d("TEST", "languageContext at start: ${context.getString(R.string.default_text)}, isAppForeground: ${isAppInForegrounded()}")
lifecycleScope.launch {
while(true) {
delay(1000L)
Log.d("TEST", "languageContext: ${context.getString(R.string.default_text)}, isAppForeground: ${isAppInForegrounded()}")
}
}
}
}
< /code>
fun isAppInForegrounded(): Boolean {
val appProcessInfo = ActivityManager.RunningAppProcessInfo()
ActivityManager.getMyMemoryState(appProcessInfo)
return appProcessInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
|| appProcessInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE
}
< /code>
And here's what I get when the app hasn’t been started for a while (its process wasn't running), cold start from QuickSettings:
15:12:43.610 D languageContext at start: Domyślny, isAppForeground: true
15:12:44.613 D languageContext: Default, isAppForeground: false // why does it return English (system default) now??? nonsense
15:12:45.633 D languageContext: Default, isAppForeground: false
15:12:46.634 D languageContext: Default, isAppForeground: false
15:12:47.636 D languageContext: Default, isAppForeground: false
15:12:48.639 D languageContext: Default, isAppForeground: false
15:12:49.641 D languageContext: Default, isAppForeground: false
15:12:50.645 D languageContext: Default, isAppForeground: false
15:12:53.018 D languageContext: Domyślny, isAppForeground: true
15:12:54.025 D languageContext: Domyślny, isAppForeground: true
15:12:55.026 D languageContext: Domyślny, isAppForeground: true
15:12:56.028 D languageContext: Domyślny, isAppForeground: true
15:12:57.030 D languageContext: Domyślny, isAppForeground: true
15:12:58.032 D languageContext: Domyślny, isAppForeground: true
15:12:59.032 D languageContext: Domyślny, isAppForeground: true
15:13:00.035 D languageContext: Domyślny, isAppForeground: true
15:13:01.069 D languageContext: Domyślny, isAppForeground: true
15:13:02.071 D languageContext: Domyślny, isAppForeground: false
15:13:03.073 D languageContext: Domyślny, isAppForeground: false
15:13:04.093 D languageContext: Domyślny, isAppForeground: false
15:13:05.094 D languageContext: Domyślny, isAppForeground: true
15:13:06.096 D languageContext: Domyślny, isAppForeground: true
15:13:07.105 D languageContext: Domyślny, isAppForeground: true
15:13:08.108 D languageContext: Domyślny, isAppForeground: true
15:13:09.109 D languageContext: Domyślny, isAppForeground: true
15:13:10.110 D languageContext: Domyślny, isAppForeground: true
15:13:11.111 D languageContext: Default, isAppForeground: false
15:13:12.112 D languageContext: Default, isAppForeground: false
15:13:13.115 D languageContext: Default, isAppForeground: false
< /code>
For some reason, it switches to the default localization (English) instead of Polish after short time.
But when I open a visible activity while the service is still running (not interrupted and not restarted), it starts returning the overridden localization instead of the default one, as it should.
It doesn't make sense to me. Another bug?
Подробнее здесь: https://stackoverflow.com/questions/796 ... ground-cau
Мобильная версия