CalWatch
CalWatch copied to clipboard
BroadcastReceiver for timezone changes
See example code: https://developer.android.com/training/wearables/watch-faces/drawing
private const val MSG_UPDATE_TIME = 0
class Service : CanvasWatchFaceService() {
...
inner class Engine : CanvasWatchFaceService.Engine() {
private lateinit var calendar: Calendar
...
// receiver to update the time zone
private val timeZoneReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
calendar.timeZone = TimeZone.getDefault()
invalidate()
}
}
// service methods (see other sections)
...
}
...
}
private fun registerReceiver() {
if (registeredTimeZoneReceiver) return
registeredTimeZoneReceiver = true
IntentFilter(Intent.ACTION_TIMEZONE_CHANGED).also { filter ->
[email protected](timeZoneReceiver, filter)
}
}
private fun unregisterReceiver() {
if (!registeredTimeZoneReceiver) return
registeredTimeZoneReceiver = false
[email protected](timeZoneReceiver)
}
Possibly handy links:
- https://stackoverflow.com/questions/3598231/timezone-example-in-broadcast-receiver
- https://stackoverflow.com/questions/16113459/timezone-changed-intent-being-received-every-few-seconds
- https://stackoverflow.com/questions/31398324/is-there-an-android-event-listener-for-change-in-the-timezone-of-the-device
- https://gist.github.com/chris-piekarski/8363023