I’ve got a clock in my widget that I’m making and I want it to update every minute in sync with the system clock. ACTION_TIME_TICK seems like the perfect solution however much of my research says it’s impossible in an AppWidget while others say there are workarounds but their very vague.
I’d prefer not to do an AlarmManager as I’d have to update very frequently to make sure that it changes minutes when the system clock changes minutes and that would drain the battery more.
Is there a workaround for ACTION_TIME_TICK or what’s the best way to update every minute in sync with the system clock with minimal battery drain?
ACTION_TIME_TICKcan only be registered viaregisterReceiver()from something that is already running. In your case, that “something” would need to be a constantly-runningService, and that’s generally an anti-pattern. Users and the OS can get rid of that service when desired.I would find a way to lightly relax the “in sync with the system clock” requirement, then use
AlarmManager. After all, Android is not a RTOS, so nothing will be “in sync with the system clock” in any guaranteed sense.Using
AlarmManager, you would specify the first alarm to be the “top” of the next minute, with a period of 60 seconds andsetRepeating(). Or, you wouldset(), scheduled for the “top” of the next minute, then schedule the next one viaset()as part of your own processing, if you think you can manually correct for drift better that way.