user1269977
user1269977

Reputation: 407

Android: ACTION_TIME_TICK in AppWidget

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?

Upvotes: 2

Views: 2087

Answers (2)

CommonsWare
CommonsWare

Reputation: 1006944

Is there a workaround for ACTION_TIME_TICK

ACTION_TIME_TICK can only be registered via registerReceiver() from something that is already running. In your case, that "something" would need to be a constantly-running Service, 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 and setRepeating(). Or, you would set(), scheduled for the "top" of the next minute, then schedule the next one via set() as part of your own processing, if you think you can manually correct for drift better that way.

Upvotes: 5

Aashish Bhatnagar
Aashish Bhatnagar

Reputation: 2605

if you just need to display it when your app runs then just update it using asyncTask but if you need it's value even in the background then using service would be the best idea

Upvotes: 0

Related Questions