CalWatch icon indicating copy to clipboard operation
CalWatch copied to clipboard

BroadcastReceiver for timezone changes

Open danwallach opened this issue 6 years ago • 0 comments

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

danwallach avatar Apr 22 '19 03:04 danwallach