views:

99

answers:

1

I have a 4x4 widget and i want to update a little piece of it every 15-20 seconds. Clearly i don't want it to be updated when the phone is in standby. The widget needs also to respond to some system events other than my timer. So, which is the best option?

  • An AlarmManager: nice but probably cpu intensive if it needs to be run every 20 seconds
  • An Handler: light but i dont know how to stop it when phone sleeps
  • A service: also here i need to understand how to stop it when phone sleeps

I will also need to update a little part of my widget without updating all its screen area, is this possible??

Thanks everybody.

+1  A: 

I have a 4x4 widget and i want to update a little piece of it every 15-20 seconds.

Please make this optional or configurable.

So, which is the best option?

A Handler has nothing to do with app widgets.

An always-running service is bad for business and is the sort of thing that causes users to attack you with task killers.

The best of the bad options is AlarmManager.

Frankly, the best answer is "don't update a little piece of it every 15-20 seconds".

CommonsWare
AlarmManager seems much more CPU intensive than a service for a 15-20 seconds update, suppose you need to create a digital clock widget, how can you update it reliably without using a service?Also article you've linked seems to point out that priority of an AlarmManager action could be too high. The only problem for the service is to reliably stop when screen is off.
JohnUopini
"suppose you need to create a digital clock widget, how can you update it reliably without using a service" -- you don't write it as a widget, but write a custom home screen app that has a digital clock. "Also article you've linked seems to point out that priority of an `AlarmManager` action could be too high." -- which is why if you are doing significant work you delegate to an `IntentService` that starts, does something in a background thread, and goes away.
CommonsWare
"The only problem for the service is to reliably stop when screen is off." -- and the memory it takes up. And the fact that users will attack you with task killers because of the memory it takes up (or at least the perceived memory it takes up). And the fact that Android will terminate your service after a while (increasingly the case with Android 2.2+), because Android is responding to users who respond to developers who write everlasting services. As I wrote originally, the best answer is "don't update a little piece of it every 15-20 seconds".
CommonsWare
With "custom home screen app" you mean an Application class that update a widget using RemoteViews? This could be a solution (especially if you have more than one widget on the screen) but how would i know that the screen is off in order to avoid updates? Thanks for your answers.
JohnUopini
@JohnUopini: "With "custom home screen app" you mean an Application class that update a widget using RemoteViews? " No, I mean an activity that replaces the user's home screen with your own, like aHome does, or the HTC Sense home screen does, or the MOTOBLUR home screen does. The reason why some HTC devices can do animations and rapid updates, for example, is because those capabilities are not done by app widgets, but by their implementation of the home screen itself.
CommonsWare