How to trigger background service when device theme is changed(Dark mode on or off) on Android

929 Views Asked by At

How to run background function when device theme is changed(Dark mode on or off) on Android. Is there any onThemeChangeListener(OnDarkModeOn) or IntentFilter or BroadcastReceiver for it?

1

There are 1 best solutions below

3
Anatolii Chub On

I'm not sure that such listener is existing. But you can check you onConfigurationChanged method:

add to the activity in your manifest file:

android:configChanges="uiMode"

And when onConfigurationChanged method will be triggered you can check new Configuration.

As it's described in the documentation: https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#config-changes

This solution will not be working when app is in the background. But you can try to use Service's onConfigurationChanged method. But it looks like a big workaround and also in this case you should be aware about Background execution limit.